在一个DIV元素中结合jquery验证错误
我使用jquery validantion创建了一些表单,并且我想在表单上方显示相同的div
元素中的所有错误。在一个DIV元素中结合jquery验证错误
我尝试这样做,但不工作:
$("#contactForm").validate({ rules: {
assunto: { required: true },
nome: { required: true, minlength: 2 },
email: { required: true, email: true },
telefone: { required: true },
mensagem: { required: true }
},
errorPlacement: function(error, element) {
if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem")
//here i think i want to insert the location but i don't know how!
else
//here i think i want to insert the location but i don't know how!
},
messages: {
assunto: {required: 'É necessário escolher um assunto'},
nome: { required: 'É necessário preencher o campo nome', minlength: 'No mínimo 2 letras' },
email: { required: 'Informe seu email', email: 'Informe um Email válido' },
telefone: { required: 'Informe seu Telefone' },
mensagem: { required: 'Escreva sua Mensagem' }
},
submitHandler: function(form){
var dados = $(form).serialize();
$.ajax({
type: "POST",
url: "enviafaleconosco.asp",
data: dados,
success: function(data)
{
alert("Obrigado! Seu contato foi enviado com sucesso. Breve retornaremos!");
$("#contactForm")[0].reset();
}
});
return false;
}
});
什么,我做错了什么?
回答:
你有一个语法错误的位置:
errorPlacement: function(error, element) { if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem")
//here i think i want to insert the location but i don't know how!
else
//here i think i want to insert the location but i don't know how!
},
如果if和else没有里面什么也没有,你必须添加大括号{}
关闭它们:
errorPlacement: function(error, element) { if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem"){
//here i think i want to insert the location but i don't know how!
} else {
//here i think i want to insert the location but i don't know how!
}
},
这也是一个很好的做法,总是添加大括号,如果块,即使他们是单线
以上是 在一个DIV元素中结合jquery验证错误 的全部内容, 来源链接: utcz.com/qa/260171.html