Jquery验证 - errorPlacement仅适用于第一次

时间:2013-11-20 12:49:18

标签: jquery jquery-mobile jquery-validate

这是我的代码

http://jqueryvalidation.org/documentation/

var validator = $("#loginForm").validate({
    errorPlacement: function (error, element) {
        error.html("");
        var img = $('<img id="dynamic">'); //Equivalent: $(document.createElement('img'))
        img.attr('src', "../images/inputerror.png");
        img.appendTo(error);
        error.insertAfter(element);
    }
});

设置图像而不是“此字段是必需的”。

这一切都很好..但只是第一次。 当我再次验证它时,它会显示“此字段再次需要”。


第一次

enter image description here


第二次

enter image description here

3 个答案:

答案 0 :(得分:2)

   jQuery.extend(jQuery.validator.messages, {
            required: "<img src='../images/inputerror.png'/>"
    });

这样做了。 感谢@Omar!

答案 1 :(得分:2)

您必须使用正确的回调函数来获得预期的行为。通常,errorPlacement仅用于自定义布局 ...就像您想要输入下方或工具提示内的所有消息一样。

如果在出现错误时切换类,或者在这种情况下切换图像,则可以使用highlightunhighlight回调函数。请参阅:http://jqueryvalidation.org/validate/

但是,您使用HTML代替邮件是一种可接受的解决方法。只需记住DOM结构...默认情况下,除非您使用<label>选项指定其他类型的元素,否则您的自定义HTML将包含在errorElement元素内。

具有默认选项的DOM结构:

<label class="error"><img src="inputerror.png" /></label>

使用errorElement: 'span'选项...

var validator = $("#loginForm").validate({
    // your options and rules,
    errorElement: 'span'
});

产生的DOM结构:

<span class="error"><img src="inputerror.png" /></span>

答案 2 :(得分:0)

为什么不用showError函数代替errorReplacement?

看一下这个例子:

  def _check_date(self,cr,uid,ids,context=None):

        record=self.browse(cr,uid,ids,context=None)
        for data in record:
            if data.start_trip >= data.start_business or data.start_trip >= data.end_business or data.start_trip >= data.end_trip or data.start_business >= data.end_business or data.start_business >= data.end_trip or data.end_business >= data.end_trip:
                return False
            else:
                return True

_constraints = [(_check_date, 'Error: You Entered Wrong Dates, Please Enter the Right Dates ',['start_trip', 'start_business', 'end_business', 'end_trip'])]
相关问题