简体   繁体   English

取消选中一个复选框,另一个复选框将取消选中

[英]Uncheck a checkbox and another checkbox will untick

I have two checkboxes in a form. 我在表单中有两个复选框。 onclick if a checkbox called email is unchecked how can I get the other checkbox to also uncheck (if it is checked) ? onclick如果未选中一个名为email的复选框,我如何才能将另一个复选框也取消选中(如果已选中)?

document.getElementById('email').onclick = function() {
     if (!this.checked) {
        !document.getElementById("other").checked;
    } else {
        // if not checked ...
    }
};

Am I completey barking up the wrong tree? 我是否完全将错误的树吠叫? Any help appriciated 任何帮助

To synchronize the checking of the both at the same time you need just to use this.checked of the first clicked one on the checked attribute of the second one like : 要同时同步两者的检查,您只需要使用this.checked在第二个对象的checked属性上单击的第一个对象,例如:

document.getElementById("other").checked = this.checked;

NOTE : That will work on one way, what means the check will be synchronized just when you click on the first checkbox that you've attached the click event to. 注意:这将以一种方式起作用,这意味着仅当您单击附加了click事件的第一个checkbox时,检查才会同步。

 document.getElementById('email').onclick = function() { document.getElementById("other").checked = this.checked; }; 
 <input id="email" type="checkbox" /> CHECKBOX 1 <br> <input id="other" type="checkbox" /> CHECKBOX 2 

In your test code you are not setting the checked property of "other" to any value. 在测试代​​码中,您未将“其他”的选中属性设置为任何值。 You are just reading its value, then inverting it (with !). 您只是在读取其值,然后将其取反(用!)。

You could try: 您可以尝试:

document.getElementById("other").checked = false;

You can make it like : 您可以像这样:

<form id="test" action="#" method="post">
    <div>
        <input type="checkbox" name="check" id="check"/>
    </div>

        <div>
        <input type="checkbox" name="check2" id="check2"/>
    </div>
</form>

document.getElementById('check').onclick = function() { document.getElementById('check')。onclick = function(){

 if (!this.checked) {
    document.getElementById("check2").checked = false;
} else {

    // other logic ...
}};

Test it online on jsfiddle : https://jsfiddle.net/3dtq0w8x/ 在jsfiddle上在线测试: https ://jsfiddle.net/3dtq0w8x/

You can add event listener to email checkbox (which is a good practice) and then check if it is check or not and deal with the other checkbox according to that 您可以将事件侦听器添加到email复选框(这是一种很好的做法),然后检查是否为选中状态,并根据该复选框处理另一个复选框。

For example 例如

 var ckb = document.getElementById('email') ckb.addEventListener("click", function(e){ if(!e.target.checked) document.getElementById('ot').checked = false; }) 
 <input type="checkbox" name="na" value="email" id="email">Email<br> <input type="checkbox" name="na" value="other" id="ot">Other 

This should help 这应该有帮助

function check() {
if(document.getElementById("email").checked){ 
    document.getElementById("other").checked = true;
}else{
    document.getElementById("other").checked = false;
}

} }

HTML 的HTML

<input type='checkbox' id='email' name='checkbox' onclick="check()" >email
<input type='checkbox' id='other' name='checkbox'>other

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

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