我有一个复选框列表,试图通过禁用选择了两个后所有未选中的复选框来限制最多只能选择2个。
这个方法很好用,但是如果用户点击被禁用的复选框时,我想向他们显示消息来告诉他们不能选择超过2个。我试图在被禁用的复选框上触发click()
事件,但它实际上并不会触发。有什么想法吗?
var totalChecked = 0;
var checkedLimit = 1;
jQuery(".post-to-facebook").change(function() {
if (jQuery(this).attr("checked") == "checked") {
if (totalChecked < checkedLimit) {
totalChecked += 1;
if (totalChecked == checkedLimit) {
jQuery(".post-to-facebook[checked!='checked']").attr("disabled", true);
}
} else {
jQuery(this).attr("checked", false);
alert("You can only post twice to Facebook at one time. This is to avoid spam complaints, we don't want to spam on your page!");
}
} else {
totalChecked -= 1;
if (totalChecked < checkedLimit) {
jQuery(".post-to-facebook[checked!='checked']").attr("disabled", false);
}
}
console.log(totalChecked);
});
// THIS DOES NOT FIRE
jQuery(".post-to-facebook:disabled").click(function() {
alert("You can only post twice to Facebook at one time. This is to avoid spam complaints, we don't want to spam on your page!");
});