gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:20:20 27 4
gpt4 key购买 nike

我在一个表单中有两个复选框。 onclick 如果取消选中名为 email 的复选框,我怎样才能让另一个复选框也取消选中(如果已选中)?

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

我是不是找错了树?任何帮助都适用

最佳答案

要同时同步两者的检查,您只需要在第二个点击的 checked 属性上使用第一个点击的 this.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

关于javascript - 取消选中一个复选框,另一个复选框将取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50061747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com