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

时间:2014-12-09 00:10:59

标签: jquery jquery-validate

jQuery Validation生成了一个范围。

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

如何仅覆盖字段2错误消息?感谢。

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,但由于我们无法看到你现在如何设置这些消息,我会给你最通用的版本......

$(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"
            }
        }
    });

});

DEMO:http://jsfiddle.net/mb2jfeLs/

答案 1 :(得分:-1)

您可以像这样在JQuery中定位该属性。

$('span[for="field2"]').text('This is the new error message for field 2');

JSFiddle