jQuery UI 对话框 OnBeforeUnload

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1889404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:20:36  来源:igfitidea点击:

jQuery UI Dialog OnBeforeUnload

jqueryjquery-ui-dialogonbeforeunload

提问by clockwiseq

I have a small problem. I'm attempting to catch the OnUnLoad Event of the Window and ask a confirmation question and if the user decides they want to stay then fine, and if they want to leave the page then they'll lose all unsaved data. Here's the issues...

我有一个小问题。我正在尝试捕获窗口的 OnUnLoad 事件并询问一个确认问题,如果用户决定他们想要留下那么好,如果他们想离开页面那么他们将丢失所有未保存的数据。这里的问题...

I'm using a jQuery UI Dialog and when I put the following code on my page, I have the Dialog open, and when I click the back button on the browser, it never pops up the msgbox. It just refreshes the page:

我正在使用 jQuery UI 对话框,当我将以下代码放在我的页面上时,我打开了对话框,当我单击浏览器上的后退按钮时,它永远不会弹出 msgbox。它只是刷新页面:

<script type="text/javascript"> 
    $(window).bind('beforeunload', function() { 
            alert('you are an idiot!'); 
        } 
    );
</script>

And the solution that I'm using was a post here. Again, the msgbox will display fine if I do not have the jQuery UI Dialog open. If I do, then it doesn't display the msgbox and just refreshes the page.

我使用的解决方案是这里的一个帖子。同样,如果我没有打开 jQuery UI 对话框,msgbox 将正常显示。如果我这样做,那么它不会显示 msgbox 而只是刷新页面。

Any ideas?

有任何想法吗?

回答by Jordan Ryan Moore

The correct way to display the alert is to simply return a string. Don't call the alert()method yourself.

显示警报的正确方法是简单地返回一个字符串。不要alert()自己调用该方法。

<script type="text/javascript">
    $(window).on('beforeunload', function() {
        if (iWantTo) {
            return 'you are an idiot!';
        }
    }); 
</script>

See also: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

另见:https: //developer.mozilla.org/en-US/docs/Web/Events/beforeunload

回答by Tomá? Mleziva

You can also make an exception for leaving the page via submitting a particular form:

您还可以通过提交特定表单来例外离开页面:

$(window).bind('beforeunload', function(){
    return "Do you really want to leave now?";
});

$("#form_id").submit(function(){
    $(window).unbind("beforeunload");
});

回答by lndr

this works for me

这对我有用

$(window).bind('beforeunload', function() {
      return 'Do you really want to leave?' ;
});

回答by Thomas

jQuery API specifically says not to bind to beforeunload, and instead should bind directly to the window.onbeforeunload, I just ran across a pretty bad memory in part due binding to beforeunload with jQuery.

jQuery API 明确指出不要绑定到 beforeunload,而是应该直接绑定到 window.onbeforeunload,我只是遇到了一个非常糟糕的内存,部分原因是使用 jQuery 绑定到 beforeunload。

回答by Lazerbeak12345

This works for me:

这对我有用:

window.addEventListener("beforeunload", function(event) {
  event.returnValue = "You may have unsaved Data";
});

回答by atik sarker

For ASP.NET MVC if you want to make an exception for leaving the page via submitting a particular form:

对于 ASP.NET MVC,如果您想通过提交特定表单来排除离开页面的情况:

Set a form id:

设置表单 ID:

@using (Html.BeginForm("Create", "MgtJob", FormMethod.Post, new { id = "createjob" }))
{
  // Your code
}



<script type="text/javascript">

  // Without submit form
   $(window).bind('beforeunload', function () {
        if ($('input').val() !== '') {
            return "It looks like you have input you haven't submitted."
        }
    });

    // this will call before submit; and it will unbind beforeunload
    $(function () {
        $("#createjob").submit(function (event) {
            $(window).unbind("beforeunload");
        });
    });

</script>