albator
albator

Reputation: 859

limit checked checkboxes jquery ui easely

Just simply want to limit the number of checkboxes, but actualy my code desallow all checkbox when lengh > 3 , whats wrong..

http://jsfiddle.net/mbAwC/11/

$('.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

Answers (4)

emerson.marini
emerson.marini

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

billyonecan
billyonecan

Reputation: 20260

Set checked to false, then call button('refresh'):

$(this).prop('checked', false).button('refresh');

Here's a fiddle

Upvotes: 0

Nikhil Patel
Nikhil Patel

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" );
        }
    });
});

JSFiddle

Upvotes: 2

lc.
lc.

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

Related Questions