简体   繁体   English

表单验证中的jQuery部分无效字段滚动

[英]jQuery partial invalid field scroll on form validation

Got a quick question about a form validation using jQuery. 有一个关于使用jQuery进行表单验证的快速问题。 So I have huge-butt form, which is validated and submitted properly. 因此,我有一个大屁股表格,该表格已正确验证并提交。 The only problem I keep running into is that when I try to submit it, and the form is invalid, the window does not scroll to the invalid field. 我一直遇到的唯一问题是,当我尝试提交表单并且表单无效时,窗口不会滚动到无效字段。 It tries to - the view sort of jumps about half an inch above the submit button and that's it - the invalid field is not actually shown on the page. 它试图-视图跳到“提交”按钮上方大约半英寸处,就是这样-无效字段实际上并未显示在页面上。 In terms of the jQuery default settings on the validator, I have the following code: 根据验证器上的jQuery默认设置,我有以下代码:

    $.extend($.validator, {
        defaults: {
        messages: {},
        groups: {},
        rules: {},
        errorClass: "error",
        validClass: "valid",
        errorElement: "label",
        focusInvalid: true,
        errorContainer: $([]),
        errorLabelContainer: $([]),
        onsubmit: true,
        ignore: ":hidden",
        ignoreTitle: false,
}

When the validator runs, this is the focusInvalid() function definition: 验证程序运行时,这是focusInvalid()函数定义:

focusInvalid: function() {
            if ( this.settings.focusInvalid ) {
                try {
                    $(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
                    .filter(":visible")
                    .focus()
                    // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
                    .trigger("focusin");
                } catch(e) {
                    // ignore IE throwing errors when focusing hidden elements
                }
            }
        },

Finally, on form validation: 最后,在表单验证中:

if ( validator.form() ) {
                    if ( validator.pendingRequest ) {
                        validator.formSubmitted = true;
                        return false;
                    }
                    return handle();
                } else {
                    validator.focusInvalid();
                    return false;
                }

focus isn't the correct function for scrolling the page to a particular element. focus不是将页面滚动到特定元素的正确功能。 You need scrollTop , or a similar function. 您需要scrollTop或类似的功能。 There are several questions about this, I like this answer which includes a simple example, and even includes the alternative solution with animation. 对此有几个问题,我喜欢这个答案 ,其中包括一个简单的示例,甚至包括动画的替代解决方案。

Thanks guys! 多谢你们! It was fixed by having the script add custom classes to the invalid forms and focusing on them. 通过使脚本将自定义类添加到无效表单并专注于它们来解决此问题。 We tried scrollTop, but that didn't work at all, so we went with a focus scenario. 我们尝试了scrollTop,但是那根本不起作用,因此我们选择了一个焦点方案。 The invalidHandler function code is below for anyone who's interested: 以下是有兴趣的任何人的invalidHandler函数代码:

    // invalidHandler to set focus to invalid controls
    invalidHandler: function(event, validator) {
        var $invalidElement = $(validator.errorList[0].element);

        if ($invalidElement.hasClass('chosen-select')) {
            $invalidElement.trigger('chosen:activate');
        } else if ($invalidElement.siblings('ul.token-input-list').length > 0) {
            var $inputToken = $invalidElement.siblings('ul.token-input-list').find('input');
            $inputToken.focus();
        }
    }

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

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