【问题标题】:bootstrap multiselect dropdown with optgroup使用 optgroup 引导多选下拉菜单
【发布时间】:2020-10-22 08:37:16
【问题描述】:

我得到了如下下拉菜单

 <select class="selectpicker" multiple>
      <optgroup label="option 1">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
      </optgroup>
      <optgroup label="option 2">
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
      </optgroup>
 </select>

我只想从一个 optgroup 中选择,当我尝试从另一个 optgroup 中选择选项时,如果一个 optgroup 已经选择了一个选项,则需要取消选择/不允许选择。我尝试了 changed.bs.select 事件来获取 opt 组索引,但不知道如何实现这一点,有什么帮助吗?

    $dropdown.on("changed.bs.select", "select", function (){
        var $option = $(this).find("option:selected", this); 
        var optGroup = $option.closest("optgroup").index(); 
     })

【问题讨论】:

    标签: jquery events bootstrap-4 bootstrap-multiselect


    【解决方案1】:

    试试这个http://jsfiddle.net/g9byn80a/

    <select id="msel" style="width:300px">
     <optgroup label="Alaskan/Hawaiian Time Zone">
        <option value="AK">Alaska</option>
        <option value="HI">Hawaii</option>
     </optgroup>
     <optgroup label="Pacific Time Zone">
        <option value="CA">California</option>
        <option value="NV">Nevada</option>
        <option value="OR">Oregon</option>
      <option value="WA">Washington</option>
     </optgroup>
    </select>
    
    <script>
    var memoryOne;
    
    $("option").mouseover(function () {
        var selectedOne = $("optgroup:nth-of-type(1)").children("option:selected").index();
        memoryOne = selectedOne;
    }).click(function () {
        var theSelectedIndex = $(this).index();
        var theParentIndex = $(this).parent().index();
        setTimeout(function () {
            clickEvent(theSelectedIndex, theParentIndex, memoryOne);
        }, 1);
    });
    
    function clickEvent(passedIndex, parentIndex, memoryOne, memoryTwo) {
        var selectionOne = memoryOne;
    
        var theParent = $("optgroup:eq(" + parentIndex + ")");
        var theOption = $("optgroup:eq(" + parentIndex + ") option:eq(" + passedIndex + ")");
        var theGrandParent = $("select");
        theParent.find("option:not(option:eq(" + passedIndex + "))").prop("selected", false);
    }
    </script>
    

    【讨论】:

    • 不工作,event.added 未定义,甚至小提琴也不是多选,还有什么帮助吗?
    • 当你只从列表中选择一个时,为什么它必须是多选的
    • 在每个 optgroup 中,我可以选择多个,但是当我在一个 optgroup 中选择任何一个或多个选项时,不应允许用户从第二个 optgroup 选项中选择任何选项
    • 为此,我建议使用包含(太平洋时区、阿拉斯加/夏威夷时区)的下拉列表,然后在第一个下拉列表中触发更改并在第二个下拉列表中填充其各个值。
    • 还是一头雾水?
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2017-07-22
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多