简体   繁体   English

互斥复选框逻辑

[英]Mutually exclusive checkbox logic

I have a button and 4 checkboxes: A, B, C and D. The button is disabled by default. 我有一个按钮和4个复选框:A,B,C和D。默认情况下该按钮为禁用状态。

  • Checking any combination of B, C and D enables the button. 选中B,C和D的任意组合将启用该按钮。

  • Checking any combination of C and D unchecks A. 选中C和D的任意组合将取消选中A。

  • Checking A unchecks C and D, and disables the button. 选中A取消选中C和D,并禁用该按钮。

  • But if A and B are both checked, then the button is enabled. 但是,如果同时选中了A和B,则将启用该按钮。

This is what I have so far. 到目前为止,这就是我所拥有的。

HTML: HTML:

<input type="checkbox" value="" name="cb1" id="1cb1">
<input type="checkbox" value="" name="cb1" id="1cb2">
<input type="checkbox" value="" name="cb1" id="1cb3">
<input type="checkbox" value="" name="cb1" id="1cb4">

<button id="b1" class="btn btn-disabled" disabled="disabled">Click me</button>

jquery jQuery的

        <script>
            $(document).ready(function(){
                $("#1cb1").click(function() {
                    if ($("#1cb2").is(":checked")) {
                     $("#b1").removeAttr("disabled").removeClass("btn-disabled");
                            $("#1cb3").attr("checked",false);
                            $("#1cb4").attr("checked",false);
                    } else {
                        $("#1cb3").attr("checked",false);
                        $("#1cb4").attr("checked",false);
                        $("#b1").attr("disabled", "disabled").addClass("btn-disabled");             
                    }
                });

                $("#1cb3, #1cb4").click(function() {
                    $("#1cb1").attr("checked",false)
                    $("#b1").removeAttr("disabled").removeClass("btn-disabled");
                });

                $("#1cb2").click(function() {
                    if ($("#1cb1").is(":checked")) {
                             $("#b1").removeAttr("disabled").removeClass("btn-disabled");
                            $("#1cb3").attr("checked",false);
                            $("#1cb4").attr("checked",false);
                    } else {
                        $("#b1").removeAttr("disabled").removeClass("btn-disabled");           
                    }
                });
            }); 
        </script>

I know it's a bit of a mess, which is why I'm asking you guys for help in finding the correct solution. 我知道那是一团糟,这就是为什么我要你们寻求正确解决方案的帮助。

Thanks in advance. 提前致谢。

To understand the tricky logic of your problem, it would have been good to know the real world background - but wth. 要了解问题的棘手逻辑,最好能了解现实世界的背景-但是要知道。 here are the ABCD checkbox blinking lights ;) 这是ABCD复选框的闪烁灯;)

  //Checking A unchecks C and D, and disables the button. function a() { if($("#A").is(":checked")) { $("#C, #D").prop("checked",false); $("#b1").prop("disabled",true); } } //Checking any combination of C and D unchecks A. function cAndD() { if($("#C").is(":checked") && $("#D").is(":checked")) { $("#A").prop("checked",false); } } //But if A and B are both checked, then the button is enabled. function aAndB() { if($("#A").is(":checked") && $("#B").is(":checked")) { $("#b1").prop("disabled",false); } } //Checking any combination of B, C and D enables the button. function bAndCAndD() { if($("#B").is(":checked") && $("#C").is(":checked") && $("#D").is(":checked")) { $("#b1").prop("disabled",false); } } //because of the "But..." ;) var rulesInOrder = { "A": [a, aAndB], "B": [aAndB, bAndCAndD], "C": [bAndCAndD, cAndD], "D": [bAndCAndD, cAndD], }; $(document).ready(function(){ $("input").click(function() { rulesInOrder[this.id].forEach(function(rule){rule();}); }); }); 
 <script src="https://code.jquery.com/jquery-2.2.3.js"></script> <label><input type="checkbox" value="" name="cbA" id="A">A</label><br> <label><input type="checkbox" value="" name="cbB" id="B">B</label><br> <label><input type="checkbox" value="" name="cbC" id="C">C</label><br> <label><input type="checkbox" value="" name="cbD" id="D">D</label><br> <button id="b1" class="btn btn-disabled" disabled="disabled">Click me</button> 

I tried to solve by jsFiddle, see if it helps you: 我试图通过jsFiddle解决,看看是否对您有帮助:

$(document).ready(function(){
   $("input[type='checkbox']").click(function() {
      var checkA = false;
      if($("#1cb1").is(":checked") && $(this).attr("id") != "1cb3" && $(this).attr("id") != "1cb4"){
        $("#1cb3").attr("checked", false);
        $("#1cb4").attr("checked", false);
        checkA = true;
      } 

      if ($("#1cb3").is(":checked") && $("#1cb4").is(":checked") && !$("#1cb2").is(":checked") && !checkA){
        $("#1cb1").attr("checked", false);
      }  

      if (($("#1cb1").is(":checked") && $("#1cb2").is(":checked"))
                || ($("#1cb2").is(":checked") && $("#1cb3").is(":checked"))
                || ($("#1cb2").is(":checked") && $("#1cb4").is(":checked"))
          || ($("#1cb3").is(":checked") && $("#1cb4").is(":checked"))) {
        $("#b1").removeAttr("disabled").removeClass("btn-disabled");
      }else{
        $("#b1").attr("disabled", "disabled").addClass("btn-disabled");
      }
    });
});

https://jsfiddle.net/lukfcb/pyyar65a/ https://jsfiddle.net/lukfcb/pyyar65a/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM