Javascript,如果继续到else之后



由于某种原因,在if之后,它继续到else,除了带你到另一个地方的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>

如果发生这种情况后,它继续进行其他操作,比如说我点击取消(这将是空的),它会说,"关闭",但随后会出现另一个弹出窗口,说,"欢迎空的,玩得开心!"然后它会把我带到其他地方,我不确定出了什么问题,如果你能帮忙,那就太好了。

使用else if而不是if

if(condition 1) {
} else if(condition 2) {
} else {
}

现在的情况是,它首先检查post是否为"Nick"无论是否,它都将继续检查post是否为"访问者"。这当然不是你想要的。如果发现第一个测试为false,则只需要执行第二个测试。

正如所写的,只有最后一个if语句具有else。只要post不是greg,您就会收到"欢迎,玩得开心"的消息。

因为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>

这样,它将只执行其中一个代码块。

或者更好,更改为使用switch语句http://jsfiddle.net/qsQvs/

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

如果只有一个IF必须发生(或其他情况),则必须使用else if。目前,其他仅适用于"greg"IF。

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"
}

小心!

您的else当前仅在最后一个if时触发。无论其他if是否触发,它都不在乎。

如果你知道它应该只是其中一个,那么在每个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

目前有7个不同的条件块,else只是最后一个条件块的一部分。在这种情况下,由于您真正想要的是具有8个分支的1个条件块,因此应该在中间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;
}

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

然而,请注意,在JavaScript(ECMAScript)规范中,您不会发现任何关于else if的提及。这不是它自己的东西。但每个else都与最近的可能没有elseif相关联,这允许您将多个条件串在一起。

换句话说,这个:

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"
        }
    }
}

我想你犯了一个小错误,试试这个代码:

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"
    }
}

这看起来更适合用于switch语句,这将大大提高可维护性。

 var post = prompt("Name?", "Visitor") 
 switch(post)
 {
 case "Nick":
        alert("Hi Nick")  
        window.location = "index.html";  
        break;
 case "Visitor":
        alert("Welcome Visitor")  
        window.location = "index.html";
        break;  
 case "":
        alert("Please Enter Name") ;
        break;
 case null:
        alert("Closing");  
        break; 
 case "show me a secret":
        window.location = "secret.html";  
        break;
 case "Greg":  
        alert("Test");  
        break;
 case "greg":
        alert("Test 1")  
        break;    
 default:
        alert("Welcome " + post + ", Have Fun!")  
        window.location = "index.html";    
 }

最新更新