-3

我有以下代码:

var aaaa = exploded[1];

if (aaaa.indexOf("bbbb")>=0) { //Do something Here }

一切都很好,但是当我添加时:

else if (aaaa.indexOf("cccc")>=0) { //Do something Else }
else if (aaaa.indexOf("dddd")>=0) { //Do something Else 2 }
else if (aaaa.indexOf("eeee")>=0) { //Do something Else 3 }

我收到一条消息“aaaa 未定义”并且代码不会运行。我怎样才能解决这个问题?

谢谢

编辑:当我在 Stacoverflow 中写到这里时,评论花括号是一个错误,它与我的问题无关。我解决了我的问题,删除了第一个 else if 中丢失的花括号。问题解决了!

4

2 回答 2

1

工作正常:

var aaaa = "bbbb";

if (aaaa.indexOf("bbbb")>=0) { 
    alert('aa') ;
}
else if (aaaa.indexOf("cccc")>=0) { 
    alert('cc');
}
else if (aaaa.indexOf("dddd")>=0) { 
    alert('dd');
}
else if (aaaa.indexOf("eeee")>=0) { 
    alert('ee');
}
于 2013-04-20T00:30:01.220 回答
1

您通过不在单独的行上使用花括号来破坏您的代码 - 因为您使用的是单行注释,所以它也在注释您的最后一个花括号。

改变:

else if (aaaa.indexOf("cccc")>=0) { //Do something Else }
else if (aaaa.indexOf("dddd")>=0) { //Do something Else 2 }
else if (aaaa.indexOf("eeee")>=0) { //Do something Else 3 }

至:

else if (aaaa.indexOf("cccc")>=0) {
    //Do something Else
}
else if (aaaa.indexOf("dddd")>=0) {
    //Do something Else 2
}
else if (aaaa.indexOf("eeee")>=0) {
    //Do something Else 3
}

除此之外,一切看起来都很好。

于 2013-04-20T00:25:10.187 回答