0

這是我多選擇下拉代碼顯示在多選擇下拉jquery的所有類別

<select class="form-control" id="lstFruits" required multiple="multiple" name="catId[]"> 
    @foreach($catlists as $category) 
    <option value="{!! $category->id !!}" >{!! $category->name !!}</option> 
    @endforeach 
</select> 
<script type="text/javascript"> 
    $(function() { 
     $('#lstFruits').multiselect({ 
      includeSelectAllOption: true 
     }); 
     $('#btnSelected').click(function() { 
      var selected = $("#lstFruits option:selected"); 
      var message = ""; 
      selected.each(function() { 
       message += $(this).text() + " " + $(this).val() + "\n"; 
      }); 
      alert(message); 
     }); 
    }); 
</script> 

問:

當我選擇超過4個類別將顯示4選擇,但我想在下拉菜單中顯示這4個類別。

它顯示此:

enter image description here

我想是這樣的,應顯示所有類別。

enter image description here

回答

0

基礎上documentation,你必須覆蓋buttonText屬性:

$('#lstFruits').multiselect({ 
    ... 
    buttonText: function(options, select) { 
     var labels = []; 
     options.each(function() { 
      if ($(this).attr('label') !== undefined) { 
       labels.push($(this).attr('label')); 
      }else { 
       labels.push($(this).html()); 
      } 
     }); 
     return labels.join(', ') + ''; 
    } 
}); 
相關問題