3 回答

TA貢獻1848條經驗 獲得超2個贊
使用change事件,您可以執行以下操作:
var limit = 3;
$('input.single-checkbox').on('change', function(evt) {
if($(this).siblings(':checked').length >= limit) {
this.checked = false;
}
});

TA貢獻1811條經驗 獲得超6個贊
嘗試這樣。
在變更事件中,
$('input[type=checkbox]').on('change', function (e) {
if ($('input[type=checkbox]:checked').length > 3) {
$(this).prop('checked', false);
alert("allowed only 3");
}
});
在JSFiddle中檢查一下

TA貢獻1863條經驗 獲得超2個贊
試試這個DEMO:
$(document).ready(function () {
$("input[name='vehicle']").change(function () {
var maxAllowed = 3;
var cnt = $("input[name='vehicle']:checked").length;
if (cnt > maxAllowed)
{
$(this).prop("checked", "");
alert('Select maximum ' + maxAllowed + ' Levels!');
}
});
});
- 3 回答
- 0 關注
- 685 瀏覽
添加回答
舉報