2016-11-28 22 views
0

我有一點的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語句說「輸入[類型=複選框]:勾選」但不如果禁用或如果class =「disabled」,這些不計入每日限制?

+0

你在哪裏存儲先前選中的複選框? –

+0

你是否檢查這個服務器端?這些值可以在用戶的​​瀏覽器中輕鬆更改。 – circusdei

回答

2

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

您可以使用:enabled

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

是否比「:enabled:checked」與「:checked」)有優勢。not(':disabled')「? – Steve

+0

@Steve:簡單?可讀性? –

+1

https://api.jquery.com/enabled-selector/ – BenG