繁体   English   中英

表单重置导致jquery.validation出现“堆栈空间不足”错误

[英]Form Reset causes “out of stack space” error with jquery.validation

我有一种形式(最简单的形式)如下:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>PC Trade Up - Test </title>
</head>
<body id="tech">
    <div id="page">
        <section id="main">
            <h2>
            Test
            </h2>
            <form action="/tech/migration/test" method="post">
                <input id="test1" name="test1" type="text" value="" />    
                <div>
                    <button type="Submit" id="formSubmit" class="btn">
                        Submit
                    </button>
                    <button type="Reset" id="formReset" class="btn">
                        Reset
                    </button>
                </div>
            </form>
        </section>
    </div>
    <script src="/Scripts/jquery-1.9.1.js"></script>
    <script src="/Scripts/jquery.validate.js"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="/content/js/lib/jquery.form.js"></script>
</body>
</html>

我可以使用此表格提交,没有任何问题。 但是,当我按下“重置”按钮时,我在IE10控制台中收到“ SCRIPT28:堆栈空间不足”。 奇怪的是,我无法在Firefox中重现它。

我通过删除jquery.form.js引用避免了该问题,但这已包含在我的捆绑软件中。 该错误似乎冒出jquery.validation.js。 我将其追溯到该脚本的第417行,内容如下:

resetForm: function () {
    if ($.fn.resetForm) {
        $(this.currentForm).resetForm();
    }
    this.submitted = {};
    this.lastElement = null;
    this.prepareForm();
    this.hideErrors();
    this.elements().removeClass(this.settings.errorClass).removeData("previousValue");
},

线

$(this.currentForm).resetForm();

似乎会在产生此错误之前递归调用254次迭代(有意义)。 但是我无法确定递归的来源。 我知道在后代上触发事件会冒泡并导致这种循环,但是我看不到我在哪里执行此操作,也看不到此脚本。

关于确定递归或循环源的任何建议?

我遇到了同样的问题,可以通过注释掉上述功能来解决它​​:

    resetForm: function() {
        if ( $.fn.resetForm ) {
            //$(this.currentForm).resetForm();
        }
        this.submitted = {};
        this.lastElement = null;
        this.prepareForm();
        this.hideErrors();
        this.elements().removeClass( this.settings.errorClass ).removeData( "previousValue" );
    },

我知道应该有一些适当的实现,但是到目前为止,此补丁解决了问题,我也在FF和Chrome中尝试过,而且似乎工作正常。

我不想更改任何现有的jquery.forms代码,因此我实现了这是在jquery.form之后加载的单独的javaScript文件。 仅应为IE加载此脚本。

(function($) {    
    $.fn.resetForm = resetForm;
    function resetForm() {
        return this.each(function() {
            // guard against an input with the name of 'reset'
            // note that IE reports the reset function as an 'object'
            if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) {
                $.fn.resetForm = undefined;
                this.reset();
                $.fn.resetForm = resetForm;
            }
        });
    };
})(jQuery);

暂无
暂无

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

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