Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
覆盖JQuery Validate生成的范围中的消息_Jquery_Jquery Validate - Fatal编程技术网

覆盖JQuery Validate生成的范围中的消息

覆盖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

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"> 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
类。