它也发生在PHP中。每当pass1进入提示弹出窗口时,它下方的警报就会像往常一样显示出来。但在那之后,其他警报框也出现了。如何在pass1上停止执行其他警报框?
function download()
{
x=prompt("Enter the download code here.")
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
if (x=="pass2")
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
}
答案 0 :(得分:5)
答案 1 :(得分:2)
尝试else if
喜欢
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // Here use else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
您也可以使用switch
之类的
switch(x) {
case "pass1" :
alert('This function has been deleted by the administrator. Jeff, get the hell out of here.');
break;
case "pass2" :
alert('I like pie too.');
break;
default :
alert('The code you specified was invalid.');
}
答案 2 :(得分:0)
您需要使用else if
function download()
{
x=prompt("Enter the download code here.")
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2")
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
}
答案 3 :(得分:0)
两件事,
如果传递 if
块执行。当它们失败时,它们会尝试找到任何关联的else
块并执行。在此之后,if
上下文将丢失。
您的代码基本上说:
如果x =='pass1' - > show离开这里吧。 其他区块不存在。
如果x =='pass2' - >告诉他你也喜欢馅饼。 (:/) 否则 - >显示代码无效的消息。
所以,基本上当有人用pass1运行代码时,会告诉他们迷路。然后,对pass2执行另一项检查,由于此失败,它们将显示无效代码错误。
解决方案,使用其他解决方案中指出的else if
语句,甚至是更好的use switch case.
答案 4 :(得分:0)
因为在你的条件if (x=="pass1")
满意所以它会提示“pass1”,
然后,如果您已经使用if (x=="pass2")
语句也会得到满足,因为这与上述条件不同。
因此,最好使用if
和else if
作为您的病情。
您的代码应该是这样的,
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // use of else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
答案 5 :(得分:0)
因为您使用了两个if
语句,但对于您的解决方案,它需要是单if
语句。
所以只需用if
替换你的第二个else if
语句。
e.g,
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}