Hello stackoverflow成员。我需要在每个未通过验证的输入下显示由jQuery validate插件生成的错误消息。如何在errorPlacement函数中设置此消息?这是我的代码。
$(document).ready(function(){
var base_url = window.location.origin;
//Preiau mesajele de erroare pentru form
var language = $("#lang").val();
var firstname = $("#fname").val();
var lastname = $("#lname").val();
var email_req = $("#em_req").val();
var email = $("#em").val();
var email_remote =$("#email_remote").val();
var country = $("#cntry").val();
var city = $("#cit").val();
base_url=base_url+"/admin/"+language+"/adduser/";
$("#success_message").hide();
$("#addnewuser").validate({
rules:{
firstname: {
required:true,
},
lastname:{
required:true,
},
email:{
required:true,
email: true,
remote: {
url: base_url+"checkemail",
type: "POST",
datatype:"JSON",
data:{
email: function(){
return $("#email").val();
}
},
}
},
country: {
required:true,
},
city:{
required:true,
},
},
messages:{
firstname: {
required:firstname,
},
lastname:{
required:lastname,
},
email:{
required:email_req,
email: email,
remote:email_remote,
},
country: {
required:country,
},
city:{
required:city,
},
},
errorPlacement:function(error,element){
},
submitHandler: function(form){
$.ajax({
type: "POST",
url:base_url+"addUser",
dataType: "html",
data: $("#addnewuser").serialize(),
success: function(data){
if(data == true)
{
$("#success_message").show();
}
}
});
return false;
}
});
});
答案 0 :(得分:2)
errorPlacement:function(error,element){
error.appendTo(element.parent().parent().after());
},
答案 1 :(得分:0)
You can do it as follows :
$("#addnewuser").validate({
invalidHandler: function(e, validator) {
var errors = "Errors found, Please check your information";
if (errors) {
$("div.error-banner").show();
$("div.success-banner").hide();
} else {
$("div.error-banner").hide();
}
},
success: function(label) {
var lblMsg = "This field is validated"
label.html(lblMsg).addClass("checked");
$(".checked").prev().addClass("checked-field");
},
highlight: function(element) {
$(element).addClass("error");
$(element).removeClass("checked-field");
$(element).next(".error").remove();
$(element).next().removeClass("checked");
},
errorPlacement: function(error, element) {
error.insertAfter(element);
},
ignore: ".hide input"
});
if($.validator){
$.validator.addMethod("cRequired", $.validator.methods.required, requiredMessage);
}
$.validator.addClassRules("required-field", {
cRequired: true
});
where requiredMessage is your variable for setting the message.