javascript编码错误,我该如何解决它

时间:2013-12-19 19:55:17

标签: javascript

window.onload=init;

function init(){
  var passwordPrompt = prompt("What is the password?");
  var passwordPrompt2 = prompt("Im sorry that was incorrect, please try again");
  if(passwordPrompt!="1234"){
    passwordPrompt2;
  }
if(passwordPrompt2!="1234"){
    passwordPrompt2
}
}
*This code is linked to an html doc*

当我输入“1234”时,它表示不正确,一旦进入prompt2,无论您输入什么,它都会接受并转到网页而不是再次运行

2 个答案:

答案 0 :(得分:2)

在您测试用户提供的输入之前,您会立即第二次提示输入密码。

看起来你正试图做这样的事情:

function init(){
  var passwordPrompt = prompt("What is the password?");
  while(passwordPrompt!="1234") {
     passwordPrompt = prompt("Im sorry that was incorrect, please try again");
  }
}

答案 1 :(得分:1)

您正在一个接一个地执行创建问题的提示。

试试这样:

function init(){
  var passwordPrompt = prompt("What is the password?");
  while(passwordPrompt!="1234") {
     passwordPrompt = prompt("Im sorry that was incorrect, please try again");
  }
}