簡體   English   中英

限制復選框,但僅限新選中的復選框

[英]Limit checkboxes but only those newly checked

我有一些jQuery,可以限制用戶可以在頁面上選中的復選框的數量:

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

但是,用戶每天只能使用一次“ x”號。 因此,他們來到表單,提交了其限制(已選中3個)。 他們第二天回來,我檢查了昨天的3個,但設置為禁用:

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

有沒有一種方法可以修改我的if語句,使其說“ input [type = checkbox]:checked”,但如果禁用則不能,如果class =“ disabled”則不可以,因此這些值不計入每日限額?

您可以像下面這樣使用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");
    }
});

你可以使用: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