由于某种原因,在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失败
目前有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
都与最近的可能没有else
的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 {
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";
}