当用户提交Html页面时,不需要的关闭确认框

时间:2016-08-09 11:51:16

标签: javascript jquery html asp.net dialog

-->

当用户关闭标签页或关闭浏览器时,我想问一个确认问题!所以我以我的形式使用了这个:

 window.onbeforeunload = function () {
                return "You're leaving the site.";
            };

但是当用户呼叫s.t或通过按钮提交或输入文本框时,对话框会再次出现!我可以阻止出现在按钮提交但问题是文本框回发!当文本框发回帖子时,如何防止onbeforeunload发生? 我试过的代码"

 $(document).ready(function () {
                $('a[rel!=ext]').click(function () { window.onbeforeunload = null; });
                $('form').submit(function () { window.onbeforeunload = null; });
            });

AND this

<form method="post" onsubmit="setFormSubmitting()">     
    <input type="submit" />
</form>

...........

var formSubmitting = false;
var setFormSubmitting = function() { formSubmitting = true; };

window.onload = function() {
    window.addEventListener("beforeunload", function (e) {
        if (formSubmitting) {
            return undefined;
        }

        var confirmationMessage = 'It looks like you have been editing something. '
                                + 'If you leave before saving, your changes will be lost.';

        (e || window.event).returnValue = confirmationMessage; //Gecko + IE
        return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
    });
};

0 个答案:

没有答案
相关问题
最新问题