JavaScript提示if / else响应

时间:2017-05-22 12:33:33

标签: javascript if-statement prompt

单击对象时,会出现提示,但提交答案时没有任何反应。密码命名有问题吗?任何建议非常感谢!



var password;
var pass1 = "thailand";
password = function password() {
  prompt('What was the first country we visited together?');
};
if (password == pass1) {
  //Change the page here
  console.log("Correct");
} else {
  console.log('sorry, try again!')
};

<div class='one'>
  <p onclick="password()">Click me!</p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要存储提示中输入的内容。此外,您还在显示提示之前在函数外部进行了比较。

我更新了代码,但您可能需要进行一些调整:

<div class='one'><p onclick="password()">Click me!</p>

<script language="JavaScript">
var pass1="thailand";
function password() {
  var pass = prompt('What was the first country we visited together?');
  if (pass == pass1)
  {
    window.location="meesha.html";
  } else { 
    console.log('sorry, try again!')
  }
}
</script>
</div>

希望这有帮助

答案 1 :(得分:0)

您不需要function password()。删除了。 并且还添加了window.location.href而不是window.location

 <div class='one'><p onclick="password()">Click me!</p>

<script language="JavaScript">
var password;
var pass1="thailand";
password = prompt('What was the first country we visited together?');
  
if (password == pass1){ 
  window.location.href = 'meesha.html';
} else { 
  console.log('sorry, try again!')
};
</script>
</div>