最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Populate state and city dropdowns based on country and state using jQuery - Stack Overflow

programmeradmin7浏览0评论

I am trying to acplish dropdowns using JSON. I want 3 dropdowns. First populate the country dropdown (eg: usa, uk etc.,). Now, when the user select USA then states dropdown needs to be populated by using jQuery .change(). Again when user select the state they need to be presented with cities dropdowns.

How can I achieve this? As my JSON file is slightly big I have added it here and tried to populate countries dropdown but unable to generate states and cities drop down...

/

$.each(myJson.country, function (index, value) {
$("#country").append('<option value="'+value.id+'">'+value.name+'</option>');});

The above code helps me populate just the countries but not others like states and cities. Any help is much appreciated.

Thanks in advance.

I am trying to acplish dropdowns using JSON. I want 3 dropdowns. First populate the country dropdown (eg: usa, uk etc.,). Now, when the user select USA then states dropdown needs to be populated by using jQuery .change(). Again when user select the state they need to be presented with cities dropdowns.

How can I achieve this? As my JSON file is slightly big I have added it here and tried to populate countries dropdown but unable to generate states and cities drop down...

http://jsfiddle/vCFv6/

$.each(myJson.country, function (index, value) {
$("#country").append('<option value="'+value.id+'">'+value.name+'</option>');});

The above code helps me populate just the countries but not others like states and cities. Any help is much appreciated.

Thanks in advance.

Share Improve this question edited Oct 17, 2018 at 9:41 Salman Arshad 272k84 gold badges442 silver badges533 bronze badges asked Jul 1, 2013 at 11:48 SpiritOfDragonSpiritOfDragon 1,4322 gold badges16 silver badges27 bronze badges 1
  • how again you are removing second options "on select of select country" – Developer Commented Feb 15, 2014 at 14:23
Add a ment  | 

4 Answers 4

Reset to default 6

You need to cascade the .change() events of the three text boxes such that:

  • Changing country:
    • Empties the state and city dropdown
    • Populates the state dropdown (asynchronously)
  • Changing state:
    • Empties the city dropdown
    • Populate the city drop down (asynchronously)

Below is a draft outline which shows how to chain the event handlers. The dropdowns are populated asynchronously so the dependent dropdowns are emptied before AJAX request.

$("#country").change(function () {
    $("#state, #city").find("option:gt(0)").remove();
    $("#state").find("option:first").text("Loading...");
    $.getJSON("/get/states", {
        country_id: $(this).val()
    }, function (json) {
        $("#state").find("option:first").text("");
        for (var i = 0; i < json.length; i++) {
            $("<option/>").attr("value", json[i].id).text(json[i].name).appendTo($("#state"));
        }
    });
});
$("#state").change(function () {
    $("#city").find("option:gt(0)").remove();
    $("#city").find("option:first").text("Loading...");
    $.getJSON("/get/cities", {
        state_id: $(this).val()
    }, function (json) {
        $("#city").find("option:first").text("");
        for (var i = 0; i < json.length; i++) {
            $("<option/>").attr("value", json[i].id).text(json[i].name).appendTo($("#city"));
        }
    });
});

Exactly like others said, you have to handle the onchange event of country & state select inputs & apply your logic. I have fiddled here for getting states dynamically on selecting a country, you might be able to code the rest yourself - Fiddle

You may also see this Populating Dropdown Based On Other Dropdown Value

You need to get the value of the "previous" dropdown, and filter out the other ones. Basicly you got two options:

  1. Have all three dropdowns with all values. Then filter out those which is not needed.
  2. Upon selecting a value in the first one, hook the change() event, make an ajax call to some backend with the selected value, and return the states and cities that corresponds to that country.

In your code you have to write all handlers when you make a selection, and have to query your JSON objects according to the input parameter and populate your further dropdowns, like you made the country. In that case you have all data on client side in JSON. Usually this kind of data are in a database, so you have to get from the server.

You can make it with JQuery Autoplete Widget.

First selection you enable only the Country autoplete.

After the user made the selection you set the state source in javascript (pass the Country parameter to the method, and when the user is typing you populates the value from the server according to the country parameter).

When the user have made the 2. selection you will enable the third autoplete, and set the input "state" parameter and populate the values according to that in the city autoplete.

发布评论

评论列表(0)

  1. 暂无评论