当提交更改时,如何避免“您确定要离开此页面吗?”关于分页在IE中点击gridview

时间:2014-06-09 09:31:24

标签: javascript jquery asp.net gridview pagination

我的网页包含带有许多输入字段的网格视图。我想在移动到下一个标签之前向用户发出警报。我的脚本在Mozilla Firefox和Chrome中运行良好,但在IE中,网格视图分页点击也会出现警报。我不想在分页点击上显示。但在Mozilla和chrome中,这并不会发生。如何避免这种情况。

我在这里给我的剧本

 <script>

    var warnMessage = "You have unsaved changes on this page!";

    $(document).ready(function () {
        $('input:not(:button,:submit),textarea,select').change(function () {
            window.onbeforeunload = function () {
                if (warnMessage != null) return warnMessage;
            }
        });
        $('input:submit').click(function (e) {
            warnMessage = null;
        });
    });



</script>

如何在gridview分页中避免此消息只在IE中单击? 有人可以帮帮我吗?

由于

1 个答案:

答案 0 :(得分:1)

在分页点击

上将值设置为null
   window.onbeforeunload = null;

更新1

尝试这一个而不是你的代码:

  var myEvent = window.attachEvent || window.addEventListener;
    var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
    myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
        var confirmationMessage = 'Are you sure to leave the page?';  // a space
        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    });

更新2

使用window.onbeforeunload = null后;在您的分页控件中重新绑定更新面板中的警报尝试此解决方案,它可以帮助您解决该问题:

   $(document).ready(function () {
     bindPageAlert();
   });

    function bindPageAlert()
    {
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
        myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
            var confirmationMessage = 'Are you sure to leave the page?';  // a space
            (e || window.event).returnValue = confirmationMessage;
            return confirmationMessage;
        });
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindPageAlert);