简体   繁体   English

jQuery验证器错误放置偏移量问题

[英]jquery validator errorPlacement offset problem

I'm trying to use errorPlacement with the Validator plugin for jquery, but I'm having issues with the offset. 我正在尝试将jquery的Validator插件与errorPlacement一起使用,但是偏移量存在问题。 If the element is hidden at the time of validation (in a different tab), the offset returns the value 0,0 (top left corner of screen). 如果在验证时隐藏了元素(在另一个选项卡中),则偏移量将返回值0,0(屏幕的左上角)。

$(document).ready(function() {

var tabs = $("#tabs").tabs();

var validator = $("#myForm").validate({

        errorElement: "div",
        wrapper: "div",  // a wrapper around the error message
        errorPlacement: function(error, element) {

            if (element.parent().hasClass('group')){
                element = element.parent();
            }


            offset = element.position();
            error.insertBefore(element)
            error.addClass('message');  // add a class to the wrapper
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
            error.css('top', offset.top);


        }
    });
}

Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗? I would recalculate the offsets when the tab changes or something, but it's being done in validate which I understood is only called once. 当选项卡更改时,我将重新计算偏移量,但这是在验证中完成的,据我了解,该调用仅被调用一次。

Thanks for the help! 谢谢您的帮助!

Edit: 编辑:

I still seem to be stuck on this. 我似乎仍然坚持这一点。 The problem I'm finding is that (at least from what I read), I can't call validate more than once. 我发现的问题是(至少从我阅读的内容来看),我不能多次调用验证。 Do you think somehow adding each error message and element key pair to a list would work, or is that a dirty hack? 您认为以某种方式将每个错误消息和元素密钥对添加到列表中是否可行,或者这是肮脏的骇客? I'm not really sure how to add them to a list anyway. 我真的不确定如何将它们添加到列表中。 Any suggestions would be much appreciated. 任何建议将不胜感激。 Thanks!! 谢谢!!

This: "The problem I'm finding is that (at least from what I read), I can't call validate more than once." 这是:“我发现的问题是(至少从我阅读的内容来看),我不能多次调用验证。” is actually not true. 实际上是不正确的。 You can manually trigger form validation any time from your javascript code, and with a little trick you can even specify what part of your form should be validated. 您可以随时通过javascript代码手动触发表单验证,并且有一点技巧,您甚至可以指定应验证表单的哪一部分。

Assuming that: 假如说:

  • you have a tabbed form, 您有一个选项卡式表格,
  • tabs are wrapped in divs 选项卡包装在div中
  • hidden tabs have css class "inactive", visible tab has css class "active" 隐藏的选项卡具有css类“无效”,可见的选项卡具有css类“有效”
  • you have forward and back button on your form for navigation along the tabs 您在表单上有前进和后退按钮,可沿选项卡导航

I'd do something like this: 我会做这样的事情:

$(document).ready(function() {

    var validator = $("#myForm").validate({
            ... // your settings
            ignore : ['#tabs .inactive input'] // tells the validator to only validate elements on your current tab
        });
    }


    $(".forward").click(function() {
        if (validator.form()) {  // triggers validation of the currently visible form inputs
                // step forward to the next tab
        }
    });
});

Also, if you're looking for a cut'n'paste solution, have a look at this multiple-step form validation example: http://jquery.bassistance.de/validate/demo/multipart/ 另外,如果您正在寻找“粘贴”解决方案,请查看此多步骤表单验证示例: http : //jquery.bassistance.de/validate/demo/multipart/

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

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