简体   繁体   English

Bootstrap 多选下拉菜单

[英]Bootstrap multiselect dropdown

I want to use a multiselect dropdown like in this example by @glyuck我想使用@glyuck 在这个例子中的多选下拉菜单

http://jsfiddle.net/surajkm33/tsomyckj/ http://jsfiddle.net/surajkm33/tsomyckj/

<select id="divRatings" class="selectpicker" multiple data-size="5" data-selected-text-format="count>2">
    <option value="All" selected="selected">All Ratings</option>
    <option value="EC">EC (Early Childhood)</option>
    <option value="E">E (Everyone)</option>
    <option value="E10+">E10+ (Everyone 10+)</option>
    <option value="T">T (Teen)</option>
    <option value="M">M (Mature)</option>
    <option value="AO">AO (Adults Only)</option>
</select>

<script>
$('#divRatings').on('change', function(){
        var thisObj = $(this);
    var isAllSelected = thisObj.find('option[value="All"]').prop('selected');
    var lastAllSelected = $(this).data('all');
    var selectedOptions = (thisObj.val())?thisObj.val():[];
        var allOptionsLength = thisObj.find('option[value!="All"]').length;
     
     console.log(selectedOptions);
      var selectedOptionsLength = selectedOptions.length;
     
    if(isAllSelected == lastAllSelected){
    
    if($.inArray("All", selectedOptions) >= 0){
        selectedOptionsLength -= 1;      
    }
            
      if(allOptionsLength <= selectedOptionsLength){
      
      thisObj.find('option[value="All"]').prop('selected', true).parent().selectpicker('refresh');
      isAllSelected = true;
      }else{       
        thisObj.find('option[value="All"]').prop('selected', false).parent().selectpicker('refresh');
         isAllSelected = false;
      }
      
    }else{          
        thisObj.find('option').prop('selected', isAllSelected).parent().selectpicker('refresh');
    }
   
        $(this).data('all', isAllSelected);
}).trigger('change');
</script>

But I want the text to be "All ratings" when all options are selected, instead of "7 items selected".但是我希望在选择所有选项时文本为“所有评级”,而不是“选择了 7 个项目”。

Any ideas on how to achieve that?关于如何实现这一目标的任何想法? I am new to JS so I don't have a clue!我是 JS 新手,所以我不知道!

您是否尝试过使用select2 之类的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM