使用jQuery验证的自定义错误标签放置(针对您的全部或部分错误) [英] Custom Error Label Placement using jQuery validate (For all or some of your errors)

查看:86
本文介绍了使用jQuery验证的自定义错误标签放置(针对您的全部或部分错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自定义位置放置一个错误标签(不是全部). jQuery提供了此 http://docs.jquery.com/Plugins/Validation/validate#toptions 选项,但是我找不到有关如何放置一条特定错误消息并且不更改所有其余错误的默认位置的任何信息?

I would like to place one error label (Not All) in a custom location. jQuery provides this http://docs.jquery.com/Plugins/Validation/validate#toptions option but I could not find anything about how to place one specific error message and not change the default location of all the rest?

请务必检查以下所有答案.有多种解决方案.谢谢大家!

Be sure to check all the answers below. There are multiple solutions to this. Thanks all!

推荐答案

因此,如果您希望将所有jQuery Validate错误消息显示在一个位置,则可以使用 http://docs.jquery.com/Plugins/Validation/validate#toptions 该页面上的(查找errorPlacement)选项.

So if you want all your jQuery Validate error messages to appear in one place you would use http://docs.jquery.com/Plugins/Validation/validate#toptions (Find errorPlacement) option on that page.

我注意到这里回答一个但不是两个选项的一些答案.

I noticed some answers on here answer one but not both options.

1) 话虽这么说,如果您想针对所有错误进行自定义放置,则可以执行以下操作:

1) That being said if you want custom placement for all of your errors you can do this:

$("#myform").validate({
  errorPlacement: function(error, element) {
     error.appendTo('#errordiv');
   }
});

2) 如果要为一个或多个错误标签指定特定位置,则可以执行此操作.

2) If you want to specify specific locations for one or multiple error labels you can do this.

errorPlacement: function(error, element) {
    if (element.attr("name") == "email" )
        error.insertAfter(".some-class");
    else if  (element.attr("name") == "phone" )
        error.insertAfter(".some-other-class");
    else
        error.insertAfter(element);
}

这篇关于使用jQuery验证的自定义错误标签放置(针对您的全部或部分错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆