简体   繁体   English

限制复选框,但仅限新选中的复选框

[英]Limit checkboxes but only those newly checked

I have a bit of jquery that lets me limit the number of checkboxes a user can check on a page: 我有一些jQuery,可以限制用户可以在页面上选中的复选框的数量:

 $('input[type=checkbox]').on('change', function (e) {
    if ($('input[type=checkbox]:checked').length > 3) {
        $(this).prop('checked', false);
        alert("allowed only 3");
    }
});

But, the user is limited to 'x' number once per day. 但是,用户每天只能使用一次“ x”号。 So they come to the form, submit their limit (3 checked). 因此,他们来到表单,提交了其限制(已选中3个)。 They come back next day, I have the 3 checked that were from yesterday but set to disabled: 他们第二天回来,我检查了昨天的3个,但设置为禁用:

 checked="checked" disabled="disabled" disabled/>

Is there a way to modify my if statement to say "input[type=checkbox]:checked" but not if disabled or not if class="disabled" so these don't count against the daily limit? 有没有一种方法可以修改我的if语句,使其说“ input [type = checkbox]:checked”,但如果禁用则不能,如果class =“ disabled”则不可以,因此这些值不计入每日限额?

You can use jQuery not() method for that like following. 您可以像下面这样使用jQuery not()方法。

$('input[type=checkbox]').on('change', function (e) {
    if ($('input[type=checkbox]:checked').not(':disabled').length > 3) {
        $(this).prop('checked', false);
        alert("allowed only 3");
    }
});

You can use :enabled 你可以使用:enabled

$('input[type=checkbox]').on('change', function (e) {
    if ($('input[type=checkbox]:enabled:checked').length > 3) {
        $(this).prop('checked', false);
        alert("allowed only 3");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM