[英]Check if a certain group of checkboxes is checked
我的页面上共有十个复选框,其中最后五个是特殊的。 如果他们中的任何一个被选中,他们都希望在页面上显示一条消息,说明这些产品将收取额外费用。 它们都有不同的名称和价值。 由于我无法在所有复选框上使用相同的 ID,因此我真的被卡住了。
这是我测试的最后一件事,但我无法为所有 5 个复选框工作。
var check;
$("#test-with-is").on("click", function(){
check = $("#mycheckbox").is(":checked");
if(check) {
alert("Checkbox is checked.");
} else {
alert("Checkbox is unchecked.");
}
});
任何线索我做错了什么?
将一个类添加到您的特殊复选框,并通过其属性“ checked”进行检查。
$(".special").on("click", function(){ if($(this).prop('checked')){ alert("Checkbox is checked."); } else { alert("Checkbox is unchecked."); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="special"> <input type="checkbox" class="special"> <input type="checkbox" class="special"> <input type="checkbox" class="special"> <input type="checkbox" class="special">
使用普通班
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" class="last_five" />
<input type="checkbox" class="last_five" />
<input type="checkbox" class="last_five" />
<input type="checkbox" class="last_five" />
并检查是否有任何检查
$("#test-with-is").on("click", function(){
if ( $(".last_five").is(":checked") ) { // true if any are checked
// do stuff
}
});
尝试使用:gt()
选择器:gt()
基于0的索引),并使用参数4
, change
事件
$("input:gt(4)").on("change", function() { if ($(this).is(":checked")) alert("extra costs will apply"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <label>1<input type="checkbox"></label> <label>2<input type="checkbox"></label> <label>3<input type="checkbox"></label> <label>4<input type="checkbox"></label> <label>5<input type="checkbox"></label> <label>6<input type="checkbox"></label> <label>7<input type="checkbox"></label> <label>8<input type="checkbox"></label> <label>9<input type="checkbox"></label> <label>10<input type="checkbox"></label>
将课程添加到您的特殊复选框
class='special_checkbox'
现在您可以检查是否有人选中了
if($('.special_checkbox:checked').length() > 0){
// checked
}
这几乎就是我要找的,但我需要检查是否选中了 6 个特定复选框中的 5 个。
有人可以帮忙吗?
如果检查 cb1、cb2、cb3、cb4 和或 cb5,则 js 警报
或者如果更容易的话,如果除 cb6 之外的所有检查都检查然后 js 警报
希望有人可以提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.