ytse_jam
ytse_jam

Reputation: 185

Populate Dropdown List based on Autocomplete Value

I have three form fields for country(text input), city(text input) and street(dropdown):

Here is my current JavaScript script:

$(".country").autocomplete("get_city.php",{ mustMatch:true })   
  .result(function (evt, data, formatted) {
    $(".city").val(data[1]);
});  

So far it's auto-filling the city(text input).

My problem now is to generate drop down list based on the filled city.

Upvotes: 1

Views: 1951

Answers (1)

Glegan
Glegan

Reputation: 110

I Think you need somthing like this:

$(".country").change(function () {

    var webMethod = "filewithdata.php";
    var parameters = { id: $('.country').val() };
    $.ajax({
        type: "GET",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            //console.log(response);

            json_obj = $.parseJSON(response);

            var output = '';

            for (var i in json_obj) {
                if (json_obj[i][4] == 1) {

                    output += '<option value="' + json_obj[i][0] + '">' + json_obj[i][0] + '</option>'
                }

            }

            $('#dropdownElement').empty().append(output);

        },
        error: function (e) {
            $(divToBeWorkedOn).html("Unavailable");
            alert("Errror:" + e);
        }
    });

});

Upvotes: 2

Related Questions