【问题标题】:If any option from a Group is selected, then all options in other group should get disabled using Bootstrap Multiselect (optgroups)如果选择了组中的任何选项,则应使用 Bootstrap Multiselect (optgroups) 禁用其他组中的所有选项
【发布时间】:2017-07-28 00:04:57
【问题描述】:

我正在使用 Bootstrap 多选 (https://davidstutz.github.io/bootstrap-multiselect/)

我在下拉列表中有 2 个组。

选择顶部组中的任何选项后,第二组中的所有项目都应被禁用。

我应该能够从第二组中选择任何选项。

我的控制器绑定数据,列表按county_category分组。 :

public void CHDSList()
    {List<SelectList> CHDSListList = new List<SelectList>();
        ViewData["CHDSList"] = new SelectList((from s in objentity.tbl_CountiesMst.ToList().OrderBy(a => a.County_Code)
                                 select new
                                  {
                                       County_Code = s.County_Code,
                                       County_Name = s.County_Code +" - " + s.County_Name,
                                       County_category = s.County_Category
                                  }), "County_Code", "County_Name", "county_category", 1);


    }

在视图中,我有这个...

$('.listbox').multiselect({
        includeSelectAllOption: false,
        buttonWidth: '100%',
        maxHeight: 200,
        enableClickableOptGroups: true   })

@Html.ListBox("ChdListBox", (SelectList)(IEnumerable<SelectListItem>)ViewData["CHDSList"],
         new { @class = "listbox", @style = "text-align:left; multiple=multiple; 
              width:100px;min-width:100%; height:30px; border-color:lightblue" })

multiselect dropdown image

【问题讨论】:

    标签: asp.net-mvc twitter-bootstrap bootstrap-multiselect


    【解决方案1】:

    您可以在多选中使用 optgroup 的 id 属性通过 jquery 禁用该组。并且不要忘记在禁用后刷新多选,如代码 sn-p 所示。 禁用后如果要选择则必须启用它。

    参考这段代码sn-p:

     
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" type="text/css"/>
    <script type="text/javascript">
     $(document).ready(function () {
            $('#multiselect').multiselect({
                onChange: function (option, checked, select) {
                    var groupId = $(option).closest('optgroup').prop('id')
                    if (groupId == "optgroup1") {
                        $('#multiselect optgroup#optgroup2').prop('disabled', true);
                        $('#multiselect').multiselect('refresh');
                    }
                }
            })
        });
    </script>
    </head>
    <body>
     <select id="multiselect" multiple="multiple">
            <optgroup label="Group 1" id="optgroup1">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
            </optgroup>
            <optgroup label="Group 2" id="optgroup2">
                <option value="4">Option 4</option>
                <option value="5">Option 5</option>
                <option value="6">Option 6</option>
            </optgroup>
        </select>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多