Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多选复选框下拉列表_Javascript_Jquery_Asp.net_Twitter Bootstrap - Fatal编程技术网

Javascript 多选复选框下拉列表

Javascript 多选复选框下拉列表,javascript,jquery,asp.net,twitter-bootstrap,Javascript,Jquery,Asp.net,Twitter Bootstrap,我正在使用multiselect复选框下拉列表 请看一个例子 选择“州”后,将显示文本值,并用逗号表示:新泽西州、纽约州、俄亥俄州 但是我想要所选项目的值,比如:NJ,NY,OH您可以使用multiselect的buttonext选项 使用multiSelect插件的按钮文本选项。参数options为您提供了所选的所有选项。然后根据需要格式化buttonText值 脚本 $(function () { $('#lstStates').multiselect({ buttonT

我正在使用multiselect复选框下拉列表

请看一个例子

选择“州”后,将显示文本值,并用逗号表示:新泽西州、纽约州、俄亥俄州


但是我想要所选项目的值,比如:NJ,NY,OH

您可以使用multiselect的
buttonext
选项


使用multiSelect插件的按钮文本选项。参数options为您提供了所选的所有选项。然后根据需要格式化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(', ');  
     }
  }); 
});

看一看fiddle:

如果不使用console polyfill或类似工具,请确保删除
console.log
语句。
$('#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(', ') + '';
        }
    }

});
$(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(', ');  
     }
  }); 
});