简体   繁体   English

JQuery验证插件:如何使错误消息成为错误标签中的属性?

[英]JQuery validation plug-in: How to make the error message an attribute in the error label?

Right now I've got the validation plug-in to display labels with one of two background images depending on if the field is valid or not. 现在,我有一个验证插件来显示带有两个背景图像之一的标签,具体取决于该字段是否有效。 However, the error text is still inserted in to the "not valid" labels (the "valid" labels don't have any error messages for them by default). 但是,错误文本仍会插入“无效”标签中(默认情况下,“有效”标签中没有任何错误消息)。

I want to remove the text from displaying on a "not valid" error label, and place it inside the label as a value for an attribute. 我想删除显示在“无效”错误标签上的文本,并将其放置在标签内作为属性值。 This way, I can use the tooltip plugin to display it as a tooltip on mouseover. 这样,我可以使用工具提示插件将其显示为鼠标悬停时的工具提示。

The problem is, I don't know how to go about doing this. 问题是,我不知道该怎么做。

I know I have to "intercept" the error messages in the messages validator option to stop them from appearing.That option can take a function for a given requirement that returns an error string (which i'll set to ""). 我知道我必须“拦截” 消息验证器选项中的错误消息以阻止它们出现。该选项可以针对给定的要求采用函数,该函数返回错误字符串(我将其设置为“”)。 However (as far as I know), I don't have access to the error label object from there to insert a message as an attribute . 但是(据我所知),我无法从那里访问错误标签对象以将消息作为attribute插入。 The description for the option states that the function takes "the element" as the second parameter, but I'm not sure if that means the DOM element or the JQuery.val rule element: 该选项的描述指出该函数将“元素”作为第二个参数,但是我不确定这是否意味着DOM元素或JQuery.val规则元素:

messages: 讯息:

Key/value pairs defining custom messages. 定义自定义消息的键/值对。 Key is the name of an element, value the message to display for that element.Instead of a plain message another map with specific messages for each rule can be used. 键是元素的名称,值该元素要显示的消息。可以使用包含每个规则的特定消息的另一个映射来代替普通消息。 Overrides the title attribute of an element or the default message for the method (in that order). 覆盖元素的title属性或方法的默认消息(按此顺序)。 Each message can be a String or a Callback. 每个消息可以是字符串或回调。 The callback is called in the scope of the validator and with the rule's parameters as the first and the element as the second arugment, it must return a String to display as the message. 在验证程序的范围内调用该回调,并以规则的参数作为第一个参数,将元素作为第二个参数,它必须返回String才能显示为消息。

I assumed it was the field DOM element and tried to do something like: 我以为这是字段DOM元素,并尝试执行以下操作:

 //#createAccount is the id for the form
 messages: {
               eMail: {
                   required: function(param,element){
                       $("#createAccount label[class=\"error\"][for=" + element.getAttribute("name") + "]")
                       .attr("message","Required"); return "";},

                   email: function(param,element){
                       $("#createAccount label[class=\"error\"][for=" + element.getAttribute("name") + "]")
                       .attr("message","Not valid email"); return "";}
                    }
           },
            success: "valid"
    ...
    ...

But for some reason it's manipulating the success validator option, which I use to choose which background image to display. 但是由于某种原因,它操纵了成功验证器选项,我用它来选择要显示的背景图像。 So it's marking things valid that it shouldn't. 因此,它标明了不应该做的事情。

I was just wondering if this was the right way to go about it, or if there is an easier way 我只是想知道这是否是正确的方法,或者是否有更简单的方法

Ok, I figured out what was wrong. 好的,我找出了问题所在。 It seems that if you submit a null string ("") as the message for an element, the plug-in will take that to mean that the field is valid. 看来,如果你提交一个空字符串(“”)作为消息的元素,插件会认为这意味着该字段是有效的。 So you can do what you have to do inside the function as long as you return anything other than "" or '' (I ended up returning "\\0", which is not a printable character) 所以,你可以做你,只要你返回以外的任何其他“功能里面做什么”或“”(我最终回到“\\ 0”,这是不是一个可打印字符)

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

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