按两次HTML表单按钮打开新页面

HTML Form button press twice to open new page

本文关键字:新页面 按钮 HTML 表单 按两次      更新时间:2023-09-26

我想做一个简单的登录使用表单,我创建了一个表单,有输入字段的用户键入密码,然后用户按提交按钮

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

<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/?密码= nopassword

然后,第二次我按下密码按钮,它会重定向到新的页面我可以知道如何解决这个问题吗?

试试这个:

<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);"/>