我在一个表格里有两个复选框。如果一个名为“电子邮件”的复选框未被选中,我如何才能让另一个复选框也取消选中(如果被选中)?
document.getElementById('email').onclick = function() {
if (!this.checked) {
!document.getElementById("other").checked;
} else {
// if not checked ...
}
};我完全是在错误的树上吠吗?任何见习帮手
发布于 2018-04-27 11:51:51
你可以让它像:
<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() {
if (!this.checked) {
document.getElementById("check2").checked = false;
} else {
// other logic ...
}};在jsfiddle:https://jsfiddle.net/3dtq0w8x/网上测试
发布于 2018-04-27 11:53:18
要同时同步两者的检查,只需对第二个属性的this.checked使用第一个单击的checked属性,如下所示:
document.getElementById("other").checked = this.checked;注意:将以一种方式工作,这意味着当您单击附加单击事件的第一个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
发布于 2018-04-27 11:51:04
在测试代码中,您没有将选中的"other“属性设置为任何值。你只是读取它的价值,然后倒置它(用!)。
你可以试试:
document.getElementById("other").checked = false;https://stackoverflow.com/questions/50061747
复制相似问题