我正在使用 jQuery 验证插件来验证我的表单。如果输入有类错误,我正在尝试向父 div 添加一些样式。
我在这里查看并找到了该invalidHandler
选项,但是当我尝试覆盖它时,验证完全停止工作。
这是我的js代码:
$(".sign_up").validate({
invalidHandler: function(form, validator) {
if (errors) {
$("errorContainer").show();
$(':input').each(function() {
if ($(this).hasClass('error')) {
$(this).parent().addClass('bg2');
}
});
}
else {
$("errorContainer").hide();
$(':input').each(function() {
$(this).parent().removeClass('bg2');
})
};
},
rules: {
"user[email]": {
required: true,
email: true,
remote: {
type: "get",
dataType: "json",
url: url_name.concat("checkemail"),
async: false
},
},
'user[password]': {
required: true,
minlength: 6
},
'user[password_confirmation]': {
equalTo: "#passs",
minlength: 6,
required: true
}
},
messages: {
"user[email]": {
remote: "This email is already taken"
},
'user[password]': {
required: "Password is required",
minlength: "Your password is too short"
},
'user[password_confirmation]': {
equalTo: "Your password confirmation doesn't match",
required: "Enter password confirmation"
}
},
onsubmit: true,
onkeydown: true,
onkeyup: false,
onfocusin: false,
onfocusout: false,
errorContainer: "#messageBox2",
errorLabelContainer: "#messageBox2 ul",
wrapper: "li"
})
我的 HTML 表单:
<form accept-charset="UTF-8" action="/users/sign_in" class="login" id="login" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
<div id="messageBox1">
<ul></ul>
</div>
<div class="field2">
<label for="user_email">E-mail Adress</label>
<input id="em" name="user[email]" size="30" type="email" value="">
</div>
<div class="field2">
<label for="user_password">Password</label>
<input class="passwords" id="user_password" name="user[password]" size="30" type="password">
</div>
<div class="submit_login">
<input class="btn go" id="check_login" name="commit" type="submit" value="Sign In">
</form>
下一个代码(与 invalidHandler 相同)正在工作,但它仅在第二次单击时添加
$("#submit").click(function(){
$(':input').each(function() {
if ($(this).hasClass('error')) {
$(this).parent().addClass('bg2');
}
});
})
我做错了什么?