【问题标题】:How to make cascading dropdown with checkbox using jquery-ajax?如何使用 jquery-ajax 使用复选框制作级联下拉菜单?
【发布时间】:2020-01-21 13:40:57
【问题描述】:

尝试使用 jquery ajax 制作级联下拉列表(国家、州、城市) 我使用了 David Stutzs 多选 jquery。 在更改国家/地区时,我使用 jquery ajax 从控制器获取状态列表并更改状态选项。 我可以根据国家/地区从控制器获取州列表,但是 $('#state').html(state);没有改变状态的内容。

当我不写 $('#state').multiselect({ 包括全选选项:真 });在开始但在 $('#state').html(state); 之后它工作一次。

我猜在应用这个 $('#state').multiselect({ 包括全选选项:真 });状态的内容没有被改变。

    <body>
<div>
    <select id="country" name="country" multiple="multiple">
        @if (ViewBag.country != null)
        {
            foreach (var item in ViewBag.country)
            {
                <option>@item</option>
            }
        }
    </select>
    <select id="state" name="state" multiple="multiple">

    </select>
</div>

<br /><br /><br /><br />
<div id="countrycontent">

</div>
<br /><br /><br /><br />
<div id="statecontent">

</div>

<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>
<script>

    $(document).ready(function () {
        //to enable multiselect dropdown with checkboxes
        $('#country').multiselect({
            includeSelectAllOption: true
        });
        $('#state').multiselect({
            includeSelectAllOption: true
        });

        //invokes when change event happens on country dropdown
        $('#country').on('change', function () {
            var selected = $("#country option:selected");
            var message = "";
            var country = [];
            var i = 0;


            selected.each(function () {
                country[i] = $(this).val();//gets selected countries
                i++;
            });

            if (country != null) {
                //gets states based on selected countries
                $.ajax({
                    type: 'post',
                    url: '../Home/GetState',
                    data: { country },
                    success: function (result) {
                        var state = '';
                        var message = '';
                        if (result != null) {
                            for (var i = 0; i < result.length; i++) {
                                message += result[i] + '<br/>';
                                state += '<option>' + result[i] + '</option>';
                            }
                        }

                        $('#countrycontent').html(message);

                        $('#state').html(state); //change state's values based on selected countries

                    }

                });
            }


        });

    });
</script>

我想要的是当国家的选择有变化时,国家的选项必须根据所选国家而改变

【问题讨论】:

    标签: jquery asp.net-mvc cascadingdropdown bootstrap-multiselect


    【解决方案1】:

    我认为你应该使用

    $('#state').multiselect('rebuild');
    

    $('#state').html(state);之后 也许会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2013-08-23
      • 1970-01-01
      • 2012-01-06
      • 2018-01-19
      • 2020-04-27
      相关资源
      最近更新 更多