我对javascript很新,我正在尝试制作一些允许人们访问我喜欢的内容的信息。例如MLP或HoO,但是如果条件似乎在第一个之后被忽略,那么我的其他如果。
var infoChoice = prompt("MLP, PJ&tO, or HoO?")
if (infoChoice != "MLP") {
if (infoChoice != "PJ&tO") {
if (infoChoice != "HoO") {
confirm(infoChoice + " is not a valid option.")
}
}
} else if (infoChoice = "MLP") {
confirm("MLP stands for My Little Pony. The 4th generation of MLP, known as MLP:FIM, (My Little Pony: Friendship is Magic) is by far the most well-known and beloved generation.")
} else if (infoChoice = "HoO") {
confirm(infoChoice + " has been registered successfully.")
}
当我运行它并搜索MLP时,它确实很好,但任何其他"有效"选项不起作用。请帮助,这非常令人沮丧。
答案 0 :(得分:4)
错误是:
if (infoChoice = "MLP")
应该是:
else if (infoChoice == "MLP")
else if (infoChoice == "HoO")
您在此处比较值,而不是分配
答案 1 :(得分:1)
如果您的可能性有限,请使用switch
声明:
switch(prompt("MLP, PJ&tO, or HoO?").toLowerCase()) {
case "mlp":
alert("MLP stands for My Little Pony. The 4th generation of MLP, known as MLP:FIM, (My Little Pony: Friendship is Magic) is by far the most well-known and beloved generation.");
break;
case "hoo":
alert(infoChoice + " has been registered successfully.");
break;
case "pj&to":
// do something?
break;
default:
alert("That is not a valid option.");
}
答案 2 :(得分:0)
我的java有点生疏,但我想尝试else if (infoChoice.equals("MLP"))
而不是使用==
运算符。原因是==
是参考比较器,而.equals则比较值。