Javascript 在一个组中只选择一个复选框,但如果是某些复选框,则可以选择多个复选框

Javascript 在一个组中只选择一个复选框,但如果是某些复选框,则可以选择多个复选框,javascript,jquery,html,Javascript,Jquery,Html,var acheckboxs=document.querySelectorAllinput[name='checkbox[]']; 变量aGroups=[ [1, 2, 3, 6], [4, 5] ]; 对于var i=0;i

var acheckboxs=document.querySelectorAllinput[name='checkbox[]']; 变量aGroups=[ [1, 2, 3, 6], [4, 5] ]; 对于var i=0;i请告诉我们你自己试过什么,我不明白你的问题。用户应该只能选择一个,但也可以选择多个?有组合选项,这就是我想说的,你有一个清单,你将有一些选项可以一起检查,而其余的只能单独检查。我发布了一个我以前尝试过的脚本。这可能是我真正需要做的一个开始,我以前没有想过将检查列表分成两组,这可能是最好的可维护方法。这段代码中还有一个bug。当您禁用第一个组中的选项,同时选中选项时,第二个组将被启用。无论哪种方式,我都会以此为起点
<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);
                    }
                });
            }
        }
    });
});