Javascript 在一个组中只选择一个复选框,但如果是某些复选框,则可以选择多个复选框
var acheckboxs=document.querySelectorAllinput[name='checkbox[]']; 变量aGroups=[ [1, 2, 3, 6], [4, 5] ]; 对于var i=0;iJavascript 在一个组中只选择一个复选框,但如果是某些复选框,则可以选择多个复选框,javascript,jquery,html,Javascript,Jquery,Html,var acheckboxs=document.querySelectorAllinput[name='checkbox[]']; 变量aGroups=[ [1, 2, 3, 6], [4, 5] ]; 对于var i=0;i
<input type="checkbox" name="checkboxes[]" value="1">
<input type="checkbox" name="checkboxes[]" value="2">
<input type="checkbox" name="checkboxes[]" value="3">
<input type="checkbox" name="checkboxes[]" value="4">
<input type="checkbox" name="checkboxes[]" value="5">
<input type="checkbox" name="checkboxes[]" value="6">
$(function () {
$('input:checkbox').on('click', function () {
var checkboxArray = $('input:checkbox'); // CheckBoxList Items
var current = $(this); // Get Selected Chec kbox
// Uncheck every checkBox that don't match the unique multi selection combo
if (current.val() != "4" && current.val() != "5") {
// Loop through checkboxArray and uncheck every item that does not meet the condition
$(checkboxArray).each(function (i) {
$(this).prop("checked", false);
});
// Check the selected item again
$(current).prop("checked", true);
} else {
// Get the current checkbox value
var chk = $(current).val();
// If the checkbox that matches the unique combo selection was click uncheck all invalid checkboxes that don't match
if (chk == "4" || chk == "5") {
// loop through the checkboxlist again
$(checkboxArray).each(function () {
// Get the looped item name
var ck = $(this).val().text();
// if checkbox does not belong to the unique combo selection, uncheck it
if (ck != "4" && ck != "5") {
$(this).prop("checked", false);
}
});
}
}
});
});