我有一个简单的登录表单,并让 jQuery 验证在显示错误时替换字段标签。问题是,一旦错误被清除,标签就会消失。我想找到一种方法来恢复到以前标签的内容,或者在该字段有效时重建该标签......
这是我的代码:
$(document).ready(function(){
$("#login").validate({
errorPlacement: function(error, element) {
element.prev().replaceWith(error);
},
rules: {
"email": {
required: true,
email:true,
},
"password": {
required: true,
minlength: 6
}
},
messages: {
email: {
required: "Please enter your email address.",
email: "Please enter a <u>valid</u> email address"
},
password: {
required: "Please enter your password.",
minlength: "Please enter a password with 6 characters or more."
}
},
});
});
和 HTML:
<form name="login" id="login" method="post" action="authenticate.php">
<p>
<label for="email">Email Address:</label>
<input type="text" name="email" id="email" class="required email" />
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" class="required" />
</p>
<p>
<input type="submit" value="Log In" id="submit" />
</p>
本质上,当用户提交表单时,如果电子邮件字段中没有数据,那么电子邮件的标签就会被错误替换。但是一旦它有有效的输入,我想放回原来的标签。
在此先感谢您的任何建议,
Z