简体   繁体   English

在复选框组合上捕获点击事件

[英]Capturing click event on combination of checkboxes

For example, I have 3 checkboxes. 例如,我有3个复选框。 I want to trigger different events bases on combinations of boxes being checked. 我想基于被选中的框的组合来触发不同的事件。 For instance, I want an click event triggered on clicking checkbox 1 and a different event based on clicking both checkbox 1 and checkbox 2 . 例如,我希望在单击复选框1时触发一个click事件,并在单击复选框1和复选框2时基于另一个事件。

REVISED CODE: 修改后的代码:

        var tSalesPro = document.getElementById("chkSalesPro");
        var tSalesProPlus = document.getElementById("chkSalesProPlus");
        var tTellerPro = document.getElementById("chkTellerPro");

        function checkBoxChecked() {

            if (tSalesPro.checked && tSalesProPlus.checked && tTellerPro.checked) {
                alert("checkboxes all checked");
            }
            else if (tSalesPro.checked && tSalesProPlus.checked) {
                alert("checkbox 1 & 2 checked");
            }
            else if (tSalesProPlus.checked && tTellerPro.checked) {
                alert("checkbox 2 & 3 checked");
                tSalesProPlus.show();
                tSalesPro.hide();
                tTellerPro.hide();
                tSalesProPlus.checked = false;
                tTellerPro.checked = false;
            }
            else if (tSalesPro.checked && tTellerPro.checked) {
                alert("checkbox 1 & 3 checked");
                tSalesPro.show();
                tSalesPro.hide();
                tSalesProPlus();
                tSalesPro.checked = false;
                tSalesProPlus.checked = false;
            }
            else if (tSalesPro.checked) {
                alert("checkbox 1 checked");
                tSalesPro.show();
                tSalesProPlus.hide();
                tTellerPro.hide();
                tSalesProPlus.checked = false;
                tTellerPro.checked = false;
            }
            else if (tSalesProPlus.checked) {
                alert("checkbox 2 checked");
                tSalesProPlus.show();
                tSalesPro.hide();
                tTellerPro.hide()
                tSalesPro.checked = false;
                tTellerPro.checked = false;
            }
            else if (tTellerPro.checked) {
                alert("checkbox 3 checked");
                tTellerPro.show();
                tSalesPro.hide();
                tSalesProPlus();
                tSalesPro.checked = false;
                tSalesProPlus.checked = false;
            }
        }

        tSalesPro.onclick = checkBoxChecked;
        tSalesProPlus.onclick = checkBoxChecked;
        tTellerPro.onclick = checkBoxChecked;

You can't click two boxes at the same time. 您不能同时单击两个框。 I would set the onclick event to be the same for all of the check boxes you're interested in, and in that function, inspect which boxes have been checked and do the appropriate processing based on that combination you find. 我会将所有您感兴趣的复选框的onclick事件设置为相同,在该功能中,检查已选中了哪些复选框,并根据找到的组合进行适当的处​​理。

Below is a simple example of showing how to use javascript and onclick events in order to determine which checkboxes are checked. 下面是一个简单的示例,显示了如何使用javascript和onclick事件来确定选中了哪些复选框。

<input type="checkbox" id="chkSalesPro">
<input type="checkbox" id="chkSalesProPlus">
<input type="checkbox" id="chkTellerPro">


<script type="text/javascript">
$(document).ready(function () {

     function checkBoxChecked() {
       var tSalesPro = $("#chkSalesPro");
       var tSalesProPlus = $("#chkSalesProPlus");
       var tTellerPro = $("#chkTellerPro");

       if (tSalesPro[0].checked && tSalesProPlus[0].checked && tTellerPro[0].checked) {
           alert("checkboxes all checked");
       }
       else if (tSalesPro[0].checked && tSalesProPlus[0].checked) {
           alert("checkbox 1 & 2 checked");
       } 
       else if (tSalesProPlus[0].checked && tTellerPro[0].checked) {
           alert("checkbox 2 & 3 checked");
       }
       else if (tSalesPro[0].checked && tTellerPro[0].checked) {
           alert("checkbox 1 & 3 checked");
       }
       else if (tSalesPro[0].checked) {
           alert("checkbox 1 checked");
       }
       else if (tSalesProPlus[0].checked) {
           alert("checkbox 2 checked");
       }
       else if (tTellerPro[0].checked) {
           alert("checkbox 3 checked");
       }
   };
   $("#chkSalesPro, #chkSalesProPlus, #chkTellerPro").change(checkBoxChecked);
});
</script>

Here is an example http://jsfiddle.net/T8YMh/5/ 这是一个示例http://jsfiddle.net/T8YMh/5/

may be you want to perform different tasks depending on checked statuses of that checkboxes? 您可能要根据该复选框的选中状态执行不同的任务? ( i mean if one of checkbox1 and checkbox2 clicked but only checkbox1 is checked do task1 and do task2 if both?) (我的意思是,如果单击了checkbox1和checkbox2中的一个,但是只选中了checkbox1,则task1和task2都被选中了吗?)

You can assign one function to "onclick" of both checkboxes and write simple logic in it: if (getElementById("checkbox1").checked && !getElementById("checkbox2").checked) { alert("first"); 您可以为两个复选框的“ onclick”分配一个函数,并在其中编写简单的逻辑:if(getElementById(“ checkbox1”)。checked &&!getElementById(“ checkbox2”)。checked){alert(“ first”); } else if (getElementById("checkbox1").checked && getElementById("checkbox2").checked) { alert("both"); }否则,如果(getElementById(“ checkbox1”)。checked && getElementById(“ checkbox2”)。checked){alert(“ both”); } }

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

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