1

我有一些 JavaScript 代码可以根据 Ajax 请求的响应对象中的各种因素创建不同的消息。例如

if (response.correct_guess === true){
    message = "your guess was correct
 }

如果用户赢得游戏,'won' 键设置为 'won'

won: "won"

因此我希望像这样设置消息

else if(response.won === "won") {
          message = "You finished with" + response.seconds + "left on the clock."

然而,这个“else if”并没有被触发

我也试过“==”(即两个等号而不是三个)。你能解释一下我做错了什么吗?

4

1 回答 1

0

你的代码是什么意思:

if (response.correct_guess === true){
    message = "your guess was correct

}else if(response.won === "won") {
      message = "You finished with" + response.seconds + "left on the clock."
}

如果猜对了,message = "your guess was correct,就是这样。如果猜测不正确,并且如果用户赢了,message = "You finished with" + response.seconds + "left on the clock."

你忘记了,如果控制进入第一个 if,它永远不会到达第二个 else if。在 if-elseif 链中,第一个 if/elseif 块控件进入是它唯一会进入的。我不认为你想要那个。你可能想要:

if (response.correct_guess === true){
    message = "your guess was correct

}
if(response.won === "won") {
      message = "You finished with" + response.seconds + "left on the clock."
}
于 2013-02-17T05:51:24.367 回答