2015-05-27 244 views
3

我正在使用多選複選框下拉菜單。多選複選框下拉菜單

請看例子jsfiddle

$(function() { $('#lstStates').multiselect({ }); }); 

一旦你選擇了狀態顯示它然後文本值和CONCAT用逗號,如:新澤西州,紐約州,俄亥俄州

但我要的是選擇的項目的VALUE如:NJ,NY,OH

回答

3

您可以使用多選的buttonText選項。

http://jsfiddle.net/ejqngpn5/

$('#lstStates').multiselect({ 
    buttonText: function(options, select) { 
     console.log(select[0].length); 
     if (options.length === 0) { 
      return 'None selected'; 
     } 
     if (options.length === select[0].length) { 
      return 'All selected ('+select[0].length+')'; 
     } 
     else if (options.length >= 4) { 
      return options.length + ' selected'; 
     } 
     else { 
      var labels = []; 
      console.log(options); 
      options.each(function() { 
       labels.push($(this).val()); 
      }); 
      return labels.join(', ') + ''; 
     } 
    } 

}); 
+0

謝謝你,它的工作。 – Maddy

+0

只要確保你刪除'console.log'語句,如果你不使用console-polyfill或類似的話。 –

+0

是的,我已經刪除它。謝謝 – Maddy

0

使用buttonText MULTISELECT插件的選項。參數選件爲您提供了所有您選擇的選項。然後根據需要格式化buttonText值。

腳本

$(function() { 
    $('#lstStates').multiselect({ 
     buttonText: function(options){ 
     if (options.length === 0) { 
      return 'No option selected ...'; 
     } 

     var labels = []; 
     options.each(function() { 
      if ($(this).attr('value') !== undefined) { 
       labels.push($(this).attr('value')); 
      } 
     }); 
     return labels.join(', '); 
    } 
    }); 
}); 

在小提琴看看:http://jsfiddle.net/74b5pkpv/