表单提交取消与Javascript不起作用



>我正在尝试构建一个注册脚本,需要验证或必须取消所有表单项。我看过很多论坛和很多答案,但我看不出我的答案有什么不同。.HTML

<form onSubmit='checkAll()' method='post' action='mydestination.php'>
<table>
  <tr>
    <td><input type='text' name='username' id='txtUsername' class='textboxLargeStyle' placeholder='Username' onChange='checkUsername("false")'/></td>
  </tr>
  <tr>
    <td><input type='text' name='email' id='txtEmail' class='textboxLargeStyle' placeholder='Email Address' onChange='checkEmail()'/></td>
  </tr>
  <tr>
    <td><input type='password' id='pwd1' name='password' class='textboxLargeStyle' placeholder='Password' onkeyup='checkPassword()'/></td>
  </tr>
  <tr>
    <td><input type='password' id='pwd2' name='password2' class='textboxLargeStyle' placeholder='Re-type Password'  onkeyup='checkPasswordMatches()'/></td>
  </tr>
  <tr>
    <td><input type='submit' class='buttonLargeStyle' value='Sign Up'/></td>
  </tr>
  <tr>
    <td colspan='2'><small id='Error'>Ensure you have all ticks before submitting! Hover over a cross to see the error.</small></td>
  </tr>
</table>
</form>

JAVASCRIPT

function checkAll()
{
if(usernameOK == true)
{
    if(emailOK == true)
    {
        if(passwordOK == true)
        {
            return true;
        }
    }
}
$('#Error').fadeIn(100);
return false;
}

当我单击提交并且不符合项目时,表单仍然提交。

您必须

onsubmit处理程序返回checkAll返回的false

<form onSubmit='return checkAll()' method='post' action='mydestination.php'>
function checkAll(e)
{
  e.stopPropogation(); //Stops bubbling
  e.preventDefault(); //prevents anything else that would happen from the click (submit)
  if(usernameOK  && (emailOK && passwordOK) {
   e.currentTarget.submit();//Submits form
  }
  $('#Error').fadeIn(100);
  return false;
}

最新更新