jQuery UI对话框OnBeforeUnload [英] jQuery UI Dialog OnBeforeUnload

查看:114
本文介绍了jQuery UI对话框OnBeforeUnload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.我试图捕获窗口的OnUnLoad事件并询问一个确认问题,如果用户确定要保留就可以了,如果他们要离开页面,则将丢失所有未保存的数据.这是问题所在...

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...

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

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:

<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.

有什么想法吗?

推荐答案

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

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

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

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

这篇关于jQuery UI对话框OnBeforeUnload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆