覆盖JQuery Validate生成的范围中的消息
jQuery验证生成了一个范围覆盖JQuery Validate生成的范围中的消息,jquery,jquery-validate,Jquery,Jquery Validate,jQuery验证生成了一个范围 <span for="field1" class="field-error"> There is an error in the field 1 </span> <span for="field2" class="field-error"> There is an error in the field 2 </span> <span for="field3" class="field-error"> Th
<span for="field1" class="field-error"> There is an error in the field 1 </span>
<span for="field2" class="field-error"> There is an error in the field 2 </span>
<span for="field3" class="field-error"> There is an error in the field 3 </span>
字段1中存在错误
字段2中有一个错误
字段3中有一个错误
如何仅覆盖字段2错误消息?谢谢。您可以像这样在JQuery中以该属性为目标
$('span[for="field2"]').text('This is the new error message for field 2');
有很多方法可以做到这一点,但由于我们无法看到您目前如何设置这些消息,因此我将为您提供最通用的版本
$(document).ready(function() {
$('#yourform').validate({
errorElement: "span",
errorClass: "field-error",
rules: {
field1: {
required: true
},
field2: {
required: true
},
field3: {
required: true
}
},
messages: {
field1: {
required: "There is an error in the field 1"
},
field2: {
required: "Some other error message"
},
field3: {
required: "There is an error in the field 3"
}
}
});
});
演示:他使用的是jQuery验证插件,因此您无法自定义验证消息。好的,谢谢您指出这一点。@user2307087如果解决了您的问题,请接受答案。谢谢,Sparky。我会考虑的。显示其余的代码。。。您对
.validate()
的调用在哪里?您现在如何设置这些消息?你的表单的HTML是什么?您确定使用的是jQuery验证插件吗?因为默认情况下,消息更通用,它们包含在label
元素中,具有error
类。