简体   繁体   English

JQuery Validator-规则特定的errorPlacement

[英]JQuery Validator- Rule specific errorPlacement

I'm using jquery validator. 我正在使用jquery验证器。

I've got a custom rule applied to a group of elements in my form. 我有一个自定义规则应用于我的表单中的一组元素。 These inputs may also have other (standard) rules associated with them. 这些输入还可能具有与之关联的其他(标准)规则。 I need to place the error message in one place if the element fails my custom rule, but another place if the element fails a standard rule. 如果元素未通过我的自定义规则,我需要将错误消息放在一个地方,如果元素未通过标准规则,则需要将错误消息放在另一个地方。

I need something like - 我需要像 -

errorPlacement: function(error, element) {
    if (error == MyCustomError) {
        // put my error at the top of the form
    } else {
        // put my error next to the element
    }

I can't see what that 'error' object really is (tried drilling down with firebug but it wasn't very informative). 我无法看到那个'错误'对象到底是什么(尝试使用firebug进行深入研究,但它没有提供足够的信息)。

The highlight option is even more difficult because I don't even have the 'error' parameter to use. 突出显示选项更加困难,因为我甚至没有使用'error'参数。

error is the whole error label HTML element that is being added by jQuery Validate, ie: error是由jQuery Validate添加的整个错误标签HTML元素,即:

<label class="error">Error message here</label>

So if you want to place the message differently if the message is "Do not do this", you can: 因此,如果您希望以不同的方式放置消息,如果消息是“不要这样做”,您可以:

errorPlacement: function(error, element) {
if (error.text() == "Do not do this") {
    error.insertBefore("SELECT FIRST ELEMENT IN THE FORM HERE USING STANDARD JQUERY SELECTORS");
} else {
    error.insertAfter(element);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM