Javascript,在if之后继续到else

Javascript,在if之后继续到else,javascript,Javascript,由于某些原因,在if之后,它继续执行其他操作,除了带您到另一个地方的if之外,下面是我目前拥有的代码 <script type="text/javascript"> function whatname(button) { var post = prompt("Name?", "Visitor") if(post == "Nick"){ alert("Hi Nick") window.locatio

由于某些原因,在if之后,它继续执行其他操作,除了带您到另一个地方的if之外,下面是我目前拥有的代码

<script type="text/javascript">
    function whatname(button) {
        var post = prompt("Name?", "Visitor")
        if(post == "Nick"){
            alert("Hi Nick")
            window.location = "index.html"
        }
        if(post == "Visitor"){
            alert("Welcome Visitor")
            window.location = "index.html"
        }
        if(post == ""){
            alert("Please Enter Name")
        }
        if(post == null){
            alert("Closing")
        }
        if(post == "show me a secret"){
            window.location = "secret.html"
        }
        if(post == "Greg"){
            alert("Test")
        }
        if(post == "greg"){
            alert("Test 1")
        }
        else{
            alert("Welcome " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
</script>

功能whatname(按钮){
var post=提示(“姓名?”,“访客”)
如果(post==“尼克”){
警惕(“你好,尼克”)
window.location=“index.html”
}
如果(post==“访客”){
警惕(“欢迎访客”)
window.location=“index.html”
}
如果(post==“”){
警报(“请输入名称”)
}
if(post==null){
警报(“关闭”)
}
如果(post==“给我看一个秘密”){
window.location=“secret.html”
}
如果(post==“Greg”){
警报(“测试”)
}
如果(post==“greg”){
警报(“测试1”)
}
否则{
提醒(“欢迎”+发布+”,祝你玩得开心!)
window.location=“index.html”
}
}

if发生后,它会继续执行其他操作,比如说我点击了cancel(该操作为空),它会说“Closing”,但另一个弹出窗口会出现,并会说“Welcome null,Have Fun!”然后它会带我去其他地方,我不确定出了什么问题,如果你能帮忙的话,如果

如果而不是
如果

if(condition 1) {
} else if(condition 2) {
} else {
}
现在的情况是,它首先检查
post
是否为“Nick”。无论是否是,它都将继续检查
post
是否为“访问者”。这当然不是你想要的。如果发现第一个测试为false,则只希望执行第二个测试


如前所述,只有最后一个
if
语句具有
else
。只要
post
不是
greg
,使用
else if
而不是
if

if(condition 1) {
} else if(condition 2) {
} else {
}
现在的情况是,它首先检查
post
是否为“Nick”。无论是否是,它都将继续检查
post
是否为“访问者”。这当然不是你想要的。如果发现第一个测试为false,则只希望执行第二个测试


如前所述,只有最后一个
if
语句具有
else
。只要
post
不是
greg
,我想你犯了一个小错误,请尝试以下代码:

function whatname(button) {
    var post = prompt("Name?", "Visitor")

    if(post == "Nick"){
        alert("Hi Nick")
        window.location = "index.html"
    }
    else if(post == "Visitor"){
        alert("Welcome Visitor")
        window.location = "index.html"
    }
    else if(post == ""){
        alert("Please Enter Name")
    }
    else if(post == null){
        alert("Closing")
    }
    else if(post == "show me a secret"){
        window.location = "secret.html"
    }
    else if(post == "Greg"){
        alert("Test")
    }
    else if(post == "greg"){
        alert("Test 1")
    }
    else{
        alert("Welcome " + post + ", Have Fun!")
        window.location = "index.html"
    }
}

我认为您犯了一个小错误,请尝试以下代码:

function whatname(button) {
    var post = prompt("Name?", "Visitor")

    if(post == "Nick"){
        alert("Hi Nick")
        window.location = "index.html"
    }
    else if(post == "Visitor"){
        alert("Welcome Visitor")
        window.location = "index.html"
    }
    else if(post == ""){
        alert("Please Enter Name")
    }
    else if(post == null){
        alert("Closing")
    }
    else if(post == "show me a secret"){
        window.location = "secret.html"
    }
    else if(post == "Greg"){
        alert("Test")
    }
    else if(post == "greg"){
        alert("Test 1")
    }
    else{
        alert("Welcome " + post + ", Have Fun!")
        window.location = "index.html"
    }
}

目前您有7个不同的条件块,
else
只是最后一个条件块的一部分。在这种情况下,因为您真正想要的是一个具有8个分支的条件块,所以您应该在中间6处使用
else if
,而不是
if

if(post == "Nick") {
   alert("Hi Nick")
   window.location = "index.html"
} else if(post == "Visitor") {
    alert("Welcome Visitor")
    window.location = "index.html"
}

    [...]

else {
    alert("Welcome " + post + ", Have Fun!")
    window.location = "index.html"
}
另外,由于您将同一个变量与一系列不同的东西进行比较,我更愿意将其视为一个开关,但这是一个品味问题:

switch(post)
{
    case "nick":
        alert("Hi Nick")
        window.location = "index.html"
        break;

    [...]

    default:
        alert("Welcome " + post + ", Have Fun!")
        window.location = "index.html"
        break;
}

目前您有7个不同的条件块,
else
只是最后一个条件块的一部分。在这种情况下,因为您真正想要的是一个具有8个分支的条件块,所以您应该在中间6处使用
else if
,而不是
if

if(post == "Nick") {
   alert("Hi Nick")
   window.location = "index.html"
} else if(post == "Visitor") {
    alert("Welcome Visitor")
    window.location = "index.html"
}

    [...]

else {
    alert("Welcome " + post + ", Have Fun!")
    window.location = "index.html"
}
另外,由于您将同一个变量与一系列不同的东西进行比较,我更愿意将其视为一个开关,但这是一个品味问题:

switch(post)
{
    case "nick":
        alert("Hi Nick")
        window.location = "index.html"
        break;

    [...]

    default:
        alert("Welcome " + post + ", Have Fun!")
        window.location = "index.html"
        break;
}

如果必须在IFs(或else)上只发生一次,则必须使用
else if
。现在,else只适用于“greg”IF


小心

如果必须在IFs(或else)上只发生一次,则必须使用
else if
。现在,else只适用于“greg”IF


小心

Your
else
当前仅在最后一个
if
未触发时触发。无论是否触发任何其他
if
s,它都不在乎

如果您知道应该只触发其中一个,请在每个If之前放置
else
s:

if(post == "Nick"){
    alert("Hi Nick")
    window.location = "index.html"
}
else if(post == "Visitor"){   //Now this will only be considered if the first failed
    alert("Welcome Visitor")
    window.location = "index.html"
}

这样,只有当allelse失败时才会考虑最终的else

您的
else
当前仅在最后一个
if
没有触发时才会触发。无论是否触发任何其他
if
s,它都不在乎

如果您知道应该只触发其中一个,请在每个If之前放置
else
s:

if(post == "Nick"){
    alert("Hi Nick")
    window.location = "index.html"
}
else if(post == "Visitor"){   //Now this will only be considered if the first failed
    alert("Welcome Visitor")
    window.location = "index.html"
}

这样,只有当allelse失败时才会考虑最后的else

一个
else
块仅与其前一个
if
,这意味着所有其他
if
块彼此独立。使用
else if
创建几个相互排斥的选项

但是,请注意,如果在JavaScript(ECMAScript)规范中提到了,那么您将不会发现任何关于的内容。这不是它自己的事情。但是每个
都有其他
,这允许您将多个条件串在一起

换言之,这:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else if (post == "Visitor") {
    alert("Welcome Visitor")
    window.location = "index.html"
}
else if (post == "") {
    alert("Please Enter Name")
}
else {
    alert("Welcome " + post + ", Have Fun!")
    window.location = "index.html"
}
…相当于:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else {
    if (post == "Visitor") {
        alert("Welcome Visitor")
        window.location = "index.html"
    }
    else {
        if (post == ""){
            alert("Please Enter Name")
        }
        else {
            alert("Welcome " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
}

else
块仅与其前一个
if
块绑定,这意味着所有其他
if
块彼此独立。使用
else if
创建几个相互排斥的选项

但是,请注意,如果在JavaScript(ECMAScript)规范中提到了
,那么您将不会发现任何关于
的内容。这不是它自己的事情。但是每个
都有其他
,这允许您将多个条件串在一起

换言之,这:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else if (post == "Visitor") {
    alert("Welcome Visitor")
    window.location = "index.html"
}
else if (post == "") {
    alert("Please Enter Name")
}
else {
    alert("Welcome " + post + ", Have Fun!")
    window.location = "index.html"
}
…相当于:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else {
    if (post == "Visitor") {
        alert("Welcome Visitor")
        window.location = "index.html"
    }
    else {
        if (post == ""){
            alert("Please Enter Name")
        }
        else {
            alert("Welcome " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
}

因为else只适用于你最后的if

<script type="text/javascript">
    function whatname(button) {
        var post = prompt("Name?", "Visitor")
        if(post == "Nick"){
            alert("Hi Nick")
            window.location = "index.html"
        }
        else if(post == "Visitor"){
            alert("Welcome Visitor")
            window.location = "index.html"
        }
        else if(post == ""){
            alert("Please Enter Name")
        }
        else if(post == null){
            alert("Closing")
        }
        else if(post == "show me a secret"){
            window.location = "secret.html"
        }
        else if(post == "Greg"){
            alert("Test")
        }
        else if(post == "greg"){
            alert("Test 1")
        }
        else{
            alert("Welcome " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
</script>

功能whatname(按钮){
var post=提示(“姓名?”,“访客”)
如果(post==“尼克”){
警惕(“你好,尼克”)
window.location=“index.html”
}
else if(post==“访客”){
警惕(“欢迎访客”)
window.loc