Populate a DropDown/Select based on the value chosen on another DropDown

后端 未结 3 716
囚心锁ツ
囚心锁ツ 2021-01-07 05:07

I\'m a beginner developer but I stumbled across a case I can\'t solve. I\'m using ASP.NET MVC with C# plus some javascript (JQuery, JSON...).

What I have to do is to

3条回答
  •  误落风尘
    2021-01-07 05:35

    You basically want to attach a javascript event onto the first drop list and have jquery query a web service (or other) to get the details for that ID. Then in javascript you re-render the second drop list with the available options. A pretty good example is here. Javascript extract from the example is below:

    $(function() {
            $.getJSON("/Home/Countries/List", function(data) {
                var items = "---------------------";
                $.each(data, function(i, country) {
                    items += "" + country.Text + "";
                });
                $("#Countries").html(items);
            });
    
        $("#Countries").change(function() {
            $.getJSON("/Home/States/List/" + $("#Countries > option:selected").attr("value"), function(data) {
                var items = "---------------------";
                $.each(data, function(i, state) {
                    items += "" + state.Text + "";
                });
                $("#States").html(items);
            });
        });
    });
    

提交回复
热议问题