jQuery验证插件:在“中显示我自己的错误”;“错误放置”;选项

jQuery验证插件:在“中显示我自己的错误”;“错误放置”;选项,jquery,Jquery,我正在使用jquery验证插件。我使用以下函数在表元素的下一个td(列)中显示默认错误消息 $(obj).find("form").validate({ errorElement: "div", wrapper: "div", errorPlacement: function(error, element) { error.appendTo( element.parent().next() ); } }); 此函数显

我正在使用jquery验证插件。我使用以下函数在表元素的下一个td(列)中显示默认错误消息

$(obj).find("form").validate({


    errorElement: "div",
            wrapper: "div",

    errorPlacement: function(error, element) {  
        error.appendTo( element.parent().next() );
    }

 });
此函数显示默认消息,但我想显示我自己的错误消息

例如,我想要这个:

<img id='error' src='images/crosssign.gif' />")Please fill this field.

提前感谢。

您可以为正在验证的字段创建消息。从:


不久前我问了同样的问题,过了一段时间我得到了答案。事实上,我想在输入字段的下一列(td)中显示一个带有标注(包含错误)的图像,该标注未经验证插件验证。当验证输入字段时,应删除此错误图像及其标注

这是我的解决办法

$("form").validate({

    errorPlacement: function(error, element) {

        //There should be an error
        if(error.html() != ''){         

            element.parent().next().html("<img id='exclamation' src='images/exclamation.gif' />").callout({
                width       : 200,
                cornerRadius    : 8,
                className   : "validationCallout",
                content     : error,
                align       : "left",
                nudgeHorizontal : -14,
                nudgeVertical   : 4,
                arrowHeight : 6
            });     
        }
    },

    success: function( label ) {
        $(obj).find(".valid").parent().next().html("");         //remove error image from next column(td) of input containing "valid" class
        $(obj).find(".valid").parent().next().closeCallout();   //remove callout on error image from next column(td) of input containing "valid" class
    }

});
$(“表单”)。验证({
errorPlacement:函数(错误,元素){
//应该有一个错误
如果(error.html()!=“”){
element.parent().next().html(“”).callout({
宽度:200,
转弯半径:8,
类名:“validationCallout”,
内容:错误,
对齐:“左”,
水平轻推:-14,
轻推垂直:4,
箭头高度:6
});     
}
},
成功:功能(标签){
$(obj).find(“.valid”).parent().next().html(“”;//从包含“valid”类的输入的下一列(td)中删除错误图像
$(obj).find(“.valid”).parent().next().closeCallout();//从包含“valid”类的输入的下一列(td)中删除错误图像上的标注
}
});
这段代码可能很复杂,但它现在正在为我工作。此处使用的标注插件与问题无关,但可能会帮助其他人。
有人能说得更简单些吗?

这不是完整的答案。我想使用我在问题中定义的errorPlacement选项。我尝试了这个“消息”选项,但它只显示默认消息。可能是我的代码有问题。如果你按照我上面列出的做,你的errorPlacement选项将是相同的。也就是说,您可以将div包装器添加到error类中,并指定其位置,然后根据需要添加图像。我的建议是浏览Phil Pafford在其答案中包含的链接,因为在那里您将找到满足您需求的完整代码。
$(".selector").validate({
   rules: {
     name: "required",
     email: {
       required: true,
       email: true
     }
   },
   messages: {
     name: "Please specify your name",
     email: {
       required: "We need your email address to contact you",
       email: "Your email address must be in the format of name@domain.com"
     }
   }
})
$("form").validate({

    errorPlacement: function(error, element) {

        //There should be an error
        if(error.html() != ''){         

            element.parent().next().html("<img id='exclamation' src='images/exclamation.gif' />").callout({
                width       : 200,
                cornerRadius    : 8,
                className   : "validationCallout",
                content     : error,
                align       : "left",
                nudgeHorizontal : -14,
                nudgeVertical   : 4,
                arrowHeight : 6
            });     
        }
    },

    success: function( label ) {
        $(obj).find(".valid").parent().next().html("");         //remove error image from next column(td) of input containing "valid" class
        $(obj).find(".valid").parent().next().closeCallout();   //remove callout on error image from next column(td) of input containing "valid" class
    }

});