简体   繁体   中英

Override jQuery custom event

I've got the following piece of code that will 'spread' an event to all my js modules. This is intended to be triggered when trying to move away from the current page. The issue is that I don't this to be triggered when the form is submitted (submit/cancel/save). Is there a way to check that?

main.js:

...

$(document).ready(function(){

    $(window).on('beforeunload', function(){
            var e = $.Event('webapp:page:closing');

            $(window).trigger(e);

            if (e.isDefaultPrevented()){
                return e.message || 'You have unsaved stuff!';
            }
        });
}

...

在提交/保存/取消之前,您可以调用以下代码,该代码将从所有侦听器中删除该处理程序

$(window).off('webapp:page:closing');

On form submit unbind the event:


$("#frm").submit(function(e) {
    $(window).off('beforeunload');
});

From MDN: The .off() method removes event handlers that were attached with .on()

我想到的唯一的办法是:与形式,当它处理的页面webapp:page:closing事件,它应该阻止默认的,如果它已经提交了一份表格(它可以在页面通过可变追踪)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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