multiselect checkbox dropdown

2020-06-04 07:48发布

问题:

I am using multiselect checkbox dropdown.

Please look example jsfiddle

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

Once you select states it show then TEXT value and concat with comma like: New Jersey, New York, Ohio

But I want VALUE of that selected ITEM like: NJ, NY, OH

回答1:

You can use buttonText option of multiselect.

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

});


回答2:

Use buttonText option of multiSelect Plugin. The Parameter options give you all the option you select. Then format your buttonText value as you want.

Script

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

Take a look at the fiddle: http://jsfiddle.net/74b5pkpv/