我希望在一个输入中有多个if-else
语句,当我使用此代码时,只有how tall is the gateway arch
会收到警报,而不是how tall are the pyramids
。
有可能吗?
document.getElementById("button").onclick = function() {
if (document.getElementById("ask").value == "how tall are the pyramids") {
alert("146.5 meters");
} else {
alert("How should I know");
}
}
if (document.getElementById("ask").value == "how tall is the gateway arch") {
alert("630 feet");
} else {
alert("How should I know");
}
}
最佳答案
您可以随意使用
这样尝试
var ask = document.getElementById("ask").value;
if (ask == "how tall are the pyramids") {
alert("146.5 meters");
} else if (ask == "how tall is the gateway arch") {
alert("630 feet");
} else {
alert("How should I know");
}
或者您可以使用
switch..case
像这样
var ask = document.getElementById("ask").value;
switch (ask) {
case "how tall are the pyramids":
alert("146.5 meters");
break;
case "how tall is the gateway arch":
alert("630 feet")
break;
default:
alert("How should I know");
}
关于javascript - 如何在JavaScript中的输入中包含多个If语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32409039/