如何在jQuery中使用unbind show方法?

时间:2018-06-14 05:31:34

标签: javascript

当用户在用户注册表单的输入字段中输入两个不同的密码时,我正在尝试显示密码不匹配错误,但在我的程序中,它在输入字段中显示错误消息。在我的程序中,unbind()方法无效。

HTML:

   <form action="adduser.php" method="POST" id="registerCandidates" enctype="multipart/form-data">
<div class="form-group">
<input class="form-control input-lg" type="password" id="password" name="password" placeholder="Password *" required>
</div>
<div class="form-group">
<input class="form-control input-lg" type="password" id="cpassword" name="cpassword" placeholder="Confirm Password *" required>
</div>
<div id="passwordError" class="btn btn-flat btn-danger hide-me" >
Password Mismatch!!
</div>
<div class="form-group">
<button type="submit" class="btn btn-flat btn-success" name="register">Register</button>
</div>
</form>

Jquery:

   <script type="text/javascript">
$(document).ready(function(){
  $("#registerCandidates").bind("submit", function(e) {
    e.preventDefault();
    if( $('#password').val() != $('#cpassword').val() ) 
    {
      $('#passwordError').show();
    }
    else 
    {
      $(this).unbind('submit').submit();
    }
  });
  });
</script>

图片(这是我的用户注册表单图片):

It is my user registration form image

1 个答案:

答案 0 :(得分:1)

你不需要解开绑定。当您想要允许表单提交时,请避免调用<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] </IfModule> 。因此,将该调用放在验证失败的e.preventDefault()分支中。

if

当用户在提交失败后编辑密码字段时,您应该隐藏错误消息。

  $("#registerCandidates").bind("submit", function(e) {
    if( $('#password').val() != $('#cpassword').val() ) 
    {
      $('#passwordError').show();
      e.preventDefault();
    }
  });

这是完整的工作代码

&#13;
&#13;
$("#password, #cpassword").on("input", function() {
    $("#passwordError").hide();
});
&#13;
$("#registerCandidates").bind("submit", function(e) {
  if ($('#password').val() != $('#cpassword').val()) {
    $('#passwordError').show();
    e.preventDefault();
  }
});

$("#password, #cpassword").on("input", function() {
  $("#passwordError").hide();
});
&#13;
.hide-me {
  display: none;
}
&#13;
&#13;
&#13;