Reputation: 859
Just simply want to limit the number of checkboxes, but actualy my code desallow all checkbox when lengh > 3 , whats wrong..
$('.limit :checkbox').change(function () {
var $cs=$(this).closest('.limit').find(':checkbox:checked');
if ($cs.length > 3) {
this.checked=false;$('.limit :checkbox').removeAttr('checked').button( "refresh" );
}
});
regards Jess
Upvotes: 0
Views: 147
Reputation: 9348
Try this way:
$(function () {
$(".limit").buttonset();
var max = 3;
var checkboxes = $('input[type="checkbox"]', '.limit');
checkboxes.change(function () {
var current = checkboxes.filter(':checked').length;
checkboxes.filter(':not(:checked)').prop('disabled', current >= max).button('refresh');
});
});
Demo: http://jsfiddle.net/mbAwC/21/
Upvotes: 0
Reputation: 20260
Set checked to false
, then call button('refresh')
:
$(this).prop('checked', false).button('refresh');
Upvotes: 0
Reputation: 1781
Just remove removeAttr('checked')
$(function() {
$(".limit").buttonset();
$('.limit :checkbox').change(function () {
var $cs=$(this).closest('.limit').find(':checkbox:checked');
if ($cs.length > 3) {
this.checked=false;
$('.limit :checkbox').button( "refresh" );
}
});
});
Upvotes: 2
Reputation: 116498
$('.limit :checkbox').removeAttr('checked')
is wrong. It will uncheck all your checkboxes.
Perhaps you meant $(this).removeAttr('checked')
(but you already have this.checked=false
)?
Upvotes: 0