这段 JavaScript 代码有什么问题?
我有两个复选框,名称为 check1 和 check2。我希望如果检查了另一项,则禁用其中一项。这就是我所做的:
var male = document.getElementById("check1");
var female = document.getElementById("check2");
male.disabled = (female.checked == true) ? true : false;
female.disabled = (male.checked == true) ? true : false;
它根本不起作用。语法是否正确。我做错了什么?
I have two checkboxes name check1 and check2. I wanted for either one to be disabled if the other one was checked. This is what I did:
var male = document.getElementById("check1");
var female = document.getElementById("check2");
male.disabled = (female.checked == true) ? true : false;
female.disabled = (male.checked == true) ? true : false;
It does not work at all. Is the syntax correct. What did I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要
onchange
事件,并且您的代码也可以得到整理。jsFiddle。
另外,你不应该使用无线电输入吗?
You need the
onchange
event, and your code could be tidied up as well.jsFiddle.
Also, shouldn't you be using radio input?
根本不应该设置
disabled
来禁用 AFAIK,任何值都是“truthy”,执行
.removeAttribute('disabled')
即可不禁用它。disabled
shouldn't be set at all in order to be disabled AFAIK, any value is "truthy"do
.removeAttribute('disabled')
to not have it disabled.单击一个复选框时,您需要更改另一个复选框的状态,例如。男性改变-女性改变,反之亦然。
无论如何,你为什么不使用
type="radio"
you need to change the state of the other checkbox when one is clicked eg. male changed - alter female, and versa verse.
Anyway why dont you use
type="radio"
尝试:
那么:
Try:
So: