JQ验证插件 - 表单外的错误标签(errorPlacement),是否可能? [英] JQ validation plugin - error labels ( errorPlacement ) outside the form, is it possible?

查看:615
本文介绍了JQ验证插件 - 表单外的错误标签(errorPlacement),是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JQuery Validation Plugin

如果可以将错误放在正在验证的FORM元素之外,我就会徘徊。我想将所有错误标签放在DOM的底部,并将它们更改为绝对位置。我试过这样的事情:

I wander if it's possible to place the error outside the FORM element which is being validated. I would like to place all the error labels at the bottom of the DOM and change them to absolute position. I tried something like this:

errorPlacement: function(error, element) {
    var position = element.offset();
        error.appendTo('body');           // put the error label before end of <body>
        error.css('position', 'absolute');
        error.css('left', position.left + element.innerWidth());
        error.css('top', position.top);
        error.addClass('error-message'); // add a class to the wrapper

这样做 - 如果输入一些错误标签,则显示正常输入无效数据,但输入有效数据后,标签不会被删除。 (我猜因为错误标签不是表单元素的子元素,验证插件根本找不到它)

What this did - the error labels appear OK if you enter some invalid data into the input, BUT after you enter valid data, the labels don't get removed. (i guess since the error label is not a child of the form element, the validation plugin simply doesn't find it)

为什么我需要它?

这是因为我有大量不同的客户端网站,它们具有不同的HTML布局和设计。有时表单位于DIV中,宽度较小,错误标签不适合div。我看到的唯一解决方案是将错误标签放在DOM的末尾,然后使用绝对位置将它们放在无效字段前面的正确位置。

Why i need it?
It's because I have a ton of different client sites that have different HTML layout and design. Sometimes the form is in a DIV with a small width and the error label doesn't fit into the div. The only solution i see is to place the error labels at the end of the DOM and then with absolute position place them at the right position, in front of the invalid fields.

如何使插件在FORM之外工作?

An suggestions how to make the plugin work outside of the FORM?

推荐答案

使用errorLabelContainer和/或errorContainer选项为您的错误消息指定目标DOM节点。喜欢:

Use the errorLabelContainer and/or errorContainer option(s) to specify the target DOM node for you error messages. Like:

$("#myform").validate({
   errorLabelContainer: "div#errors",
})

请参阅 http://docs.jquery.com/Plugins/Validation/validate#toptions ,你会发现一些使用errorContainer的工作示例和errorLabelContainer。

See the documentation at http://docs.jquery.com/Plugins/Validation/validate#toptions , you'll find some working examples both for using errorContainer and errorLabelContainer.

这篇关于JQ验证插件 - 表单外的错误标签(errorPlacement),是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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