开发者

How to populate a dropdownlist based on the selection made in another dropdownlist?

开发者 https://www.devze.com 2023-01-24 06:52 出处:网络
How do I automatically populate the second dropdown based on the selection made in the first one. Say if I choose a contry in the first dropdown, then I want to display a list of cities from that 开发

How do I automatically populate the second dropdown based on the selection made in the first one. Say if I choose a contry in the first dropdown, then I want to display a list of cities from that 开发者_开发技巧country in the second dropdown. Is there a way to do this with jQuery?


You have to use AJAX

$("#select1").change(function() {
    var optionId = $(this).val();
    $.post('ajax/getData.php',
           {id: optionId}, 
           function(data) {
              $("#div2").html(data);
           }, 
           "json"
    );
});

Here is example HTML:

<div id="div1">
    <select id="select1">
        <option value="1">aaaaa</option>
        ....
    </select>
</div>
<div id="div2">

</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号