我在Stack上四处寻找,试图找到我在jQuery验证中遇到的问题的解决方案。我已经决定将我的输入字段下方的错误消息更改为放置在表单上方的单个通用全包含消息。 我有一个"隐藏" 这个消息" #error_mail" ,我想用&#34替换;错误" 当提交/验证字段中存在错误时。 非常感谢任何帮助,提前谢谢。
HTML
<form action="#" method="post" class="new_web_user" id="new_web_user" autocomplete="off">
<p class="fields-required">
*Required fields</p>
<p id="error_mail" class="hide">We're sorry, the fields highlighted in red are required</p>
<div class="field-column"> etc etc.....
JS
$(function(){jQuery.validator.setDefaults({
errorContainer: "#error_mail",
errorPlacement: function(error, element) {
error.appendTo('#error_mail');
},
ignore: ':hidden:not([class~=selectized]),:hidden > .selectized, .selectize-control .selectize-input input'
});
$('#new_web_user').validate({
rules: {
web_user_title: {required: true},
web_user_given_name: {
required: true
},
web_user_family_name: {
required: true
},
web_user_email: {
required: true,
email: true
},
email_confirmation: {
required: true,
equalTo: '#web_user_email'
},
web_user_country: {
required: false,
},
},
messages : {
web_user_title: {
required: "Please select a title."
},
web_user_given_name: {
required: "Firstname is required."
},
web_user_family_name: {
required: "Lastname is required."
},
web_user_email: {
required: "Email address is required.",
email: "Email address seems invalid."
},
email_confirmation: {
required: "Email address is required.",
equalTo: "Email addresses do not match."
},
web_user_country: {
required: "Please choose your country."
}
},
submitHandler: function(form) {
$.ajax({
答案 0 :(得分:1)
您可以使用jQuery Validate:
中提供的选项对此进行简化$("#new_web_user").validate({
errorContainer: '#error_mail',
errorLabelContainer: '#actual_errors',
wrapper:'li',
//your rules and messages here
});
然后,将您的HTML更改为更像这样的内容:
<p id="error_mail" class="hide error">
We're sorry, the fields highlighted in red are required
<ul id="actual_errors"></ul>
</p>
请注意,不要在#error_mail
上添加/删除您想要的错误类,只需在任何时候设置它,因为只有在出现问题时才能看到它...