[英]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.