簡體   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