There are selectpicker
beside checkbox
. I want if checkbox is checked, selectpicker will be enable, if unchecked, selectpicker will be disable.
I wrote this which was not working ( Here is the fiddle ):
$('.checkBox').on('ifChecked', function(event) {
$(this).parents('.clearfix').find('.selectpicker').removeAttr('disabled');
});
$('.checkBox').on('ifUnchecked', function(event) {
$(this).parents('.clearfix').find('.selectpicker').attr('disabled');
});
If your select picker has more than just a few options, the current accepted answer is incredibly slow. (can cause a hang around half a second, which is way too long to just make something disabled.)
This worked for me:
Disable:
$("#yourSelect").prop("disabled", true);
$(".selectpicker[data-id='yourSelect']").addClass("disabled");
Enable:
$("#yourSelect").prop("disabled", false);
$(".selectpicker[data-id='yourSelect']").removeClass("disabled");
This also has the added bonus of actually showing what the value of the select was when it was disabled. (which is the behavior of select boxes)
I'm kind of surprised the official documentation suggests to use refresh
just to disable it, it takes way too long.
来源:https://stackoverflow.com/questions/27317296/how-to-enable-disable-bootstrap-selectpicker-by-ickeck-checkbox