0

我想使用表单进行简单的登录,我创建了一个表单,其中包含输入字段供用户输入密码,然后用户按下提交按钮

密码将使用 javascrip 检索,如果密码正确,它将打开一个新页面

<script language="javascript">
   function check(form)/*function to check userid & password*/
   {
 /*the following code checkes whether the entered userid and password are matching*/
       if(form.password.value == "nopassword")
       {
           self.location='main.html'
       }
           else
       {
           alert("Wrong Password\nTry again Bak Choy Friend! :)")/*displays error message*/
       }
    }
</script>
.
.
.
<form>
  <input type="text" placeholder="Password" name="password">
  <input type="button" class="button" value="Login" onclick="check(this.form)"/>                  
</form>

但问题是它需要用户按两次而不是一次才能打开新页面?第一次,我按密码按钮,地址栏显示

...net/?password=nopassword

然后,我第二次按密码按钮,它会将我重定向到新页面,请问我该如何解决这个问题?

4

1 回答 1

0

试试这个 :

<script language="javascript">
function check(form)/*function to check userid & password*/
{
 /*the following code checkes whether the entered userid and password are matching*/
   if(form.password.value === "nopassword")
   {
      self.location = 'main.html';
      return true;
   }
       else
   {
       alert("Wrong Password\nTry again Bak Choy Friend! :)")/*displays error message*/
       return false;
   }
}
</script>

 <input type="button" class="button" value="Login" onclick="return check(this.form);"/>
于 2013-03-30T08:24:29.123 回答