且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery验证是否将类添加到错误元素?

更新时间:2023-12-02 23:36:22

当您指定errorClass时,没有什么可以阻止您将其变为两件事:帮助内联错误",即:

When you specify the errorClass, there's nothing stopping you from making it two things: "help-inline error", i.e.:

$("#register").validate({
    debug: true,
    errorClass: 'error help-inline',
    validClass: 'success',
    errorElement: 'span',
    highlight: function(element, errorClass, validClass) {
      $(element).parents("div.control-group").addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function(element, errorClass, validClass) {
      $(element).parents(".error").removeClass(errorClass).addClass(validClass);
    }
});

请注意,您必须使用jQuery Validate 1.10版本来支持此功能.早期版本仅允许一个errorClass.

Note that you have to use version 1.10 of jQuery Validate to support this. Earlier versions only allow one errorClass.