How to set a parameter for window selection

I have 2 select elements in my code. One for US states ( #us_state ) and one for cities ( #city_name ). When I select "state", the second select element should contain only those cities that exist in the selected state. I use ajax, I fulfilled the request and got a response. The response format is id: name {"26332":"Abbott Park","27350":"Addieville", ...}. Now I want to add an answer to the elements parameter and add it to select element c #city_name id. But I do not know how to achieve this result.<option value="key">value</option>

$('#us_state').bind('change', function() {
    var projectId = $("#inputtitle").val();     
    var stateId =  $(this).val(); 
    var arrayData = {"projectId": projectId, "stateId":stateId};
    $.ajax({
          type: "GET",
          url: "/bidboldly/projects/editproject/",        
          data: arrayData,
          success : function(response) {
             ............
             ..........                         
          },
          error: function(){
            alert("error");
          }
    })
});
+3
source share
2 answers

Is this what you are looking for?

data = eval('( '+response' )'); //Parse the JSON data
for (zip in data) { //Iterate through each item in data
    $('#city_name').append('<option value="'+zip+'">'+data[zip]+'</option>'); //Append option to select element
}
+1
source

, , var city. jquery append ( $('#city_name').html("");)

$('#city_name').append('<option value="'+city+'">'+city+'</option>');
+2

All Articles