JavaScript - 使用“选中/未选中复选框”作为“IF ELSE 条件语句”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32589660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript - Using "checked/unchecked checkbox" as "IF ELSE condition statement"
提问by Hudoyo Danumurti
Now I want to use "check/unchecked checkbox" as "IF condition statement".
现在我想使用“选中/未选中复选框”作为“IF 条件语句”。
Example:I want to make a chord analyzer based on the note(s) you hit. When you check a note (or some notes) on checkbox, then press the button under those checkboxes, the paragraph element should tell you about what chord you hit. But there's error on my writing at if ("error")
statement. How to fix it?
示例:我想根据您弹奏的音符制作和弦分析器。当您在复选框上选中一个音符(或一些音符),然后按下这些复选框下方的按钮时,段落元素应该告诉您您击中的和弦。但是我写的 atif ("error")
语句有错误。如何解决?
Here's my code:
这是我的代码:
<!doctype html>
<html>
<head>
<script>
function fungsi() {
var c = document.getElementById("C").checked;
var e = document.getElementById("E").checked;
var f = document.getElementById("F").checked;
var g = document.getElementById("G").checked;
if (c.checked = true &&
e.checked = true &&
g.checked = true &&
f.checked = false) {
document.getElementById("chord").innerHTML = "C Chord";
} else if (c.checked = true &&
f.checked = true &&
g.checked = false &&
e.checked = false) {
document.getElementById("chord").innerHTML = "F Chord";
} else {
alert("Under Development!");
}
}
</script>
</head>
<body>
<form>
Note(s) That You Hit:<br>
<input type=checkbox id="C">C<br>
<input type=checkbox id="E">E<br>
<input type=checkbox id="F">F<br>
<input type=checkbox id="G">G
</form>
<button type=button onclick="fungsi()">The Chord Is</button>
<p id="chord"></p>
</body>
</html>
采纳答案by T.J. Crowder
if (c.checked=true)
and similar should be simply if (c.checked)
.
if (c.checked=true)
和类似的应该是简单的if (c.checked)
。
if (f.checked=false)
and similar should be simply if (!f.checked)
.
if (f.checked=false)
和类似的应该是简单的if (!f.checked)
。
If you want, you can do if (c.checked==true)
or if (c.checked===true)
, and similarly if (f.checked==false)
or if (f.checked===false)
, but it's pointless the vast majority of the time. You already havea boolean value. Just if (c.checked)
and if (!f.checked)
is sufficient.
如果你愿意,你可以做if (c.checked==true)
or if (c.checked===true)
,类似的if (f.checked==false)
or if (f.checked===false)
,但在绝大多数情况下都是没有意义的。您已经有了一个布尔值。只是if (c.checked)
和if (!f.checked)
就足够了。
E.g.:
例如:
if (c.checked &&
e.checked &&
g.checked &&
!f.checked) {
document.getElementById("chord").innerHTML = "C Chord";
}
Separately, as crclayton points out, you either want:
另外,正如crclayton 指出的那样,您要么想要:
var c=document.getElementById("C").checked;
var d=document.getElementById("E").checked;
// ...and so on..., and then:
if (c && e && g && !f)
or
或者
var c=document.getElementById("C"); // <== No .checked
var d=document.getElementById("E"); // <==
// ...and so on..., and then:
if (c.checked &&
e.checked &&
g.checked &&
!f.checked) {