javascript - 提交更改时如何避免 “Are you sure you want to navigate away from this page?”?在 IE 中点击 gridview 进行分页

标签 javascript jquery asp.net gridview pagination

我的网页包含带有许多输入字段的 GridView 。我想在移动到下一个选项卡之前向用户发出警报。我的脚本在 Mozilla Firefox 和 chrome 中工作正常,但在 IE 中, GridView 分页点击也会出现警报。我不想在分页点击上显示。但在 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>

如何避免仅在 IE 中点击 gridview 分页时出现此消息? 有人可以帮我吗?

谢谢

最佳答案

只需在分页点击时将值设置为 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; 后在分页控件中重新绑定(bind)更新面板中的警报尝试此解决方案,它可能会帮助您解决该问题:

   $(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);

关于javascript - 提交更改时如何避免 “Are you sure you want to navigate away from this page?”?在 IE 中点击 gridview 进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117366/

相关文章:

javascript/jquery 值 .get().attr()

asp.net - VB.NET 对 ASP.NET 5 (MVC6) 的支持

javascript - 如何遍历 GeoJSON 属性以显示在 Leaflet 标记中?

jquery - 限制可见时间线图表的X轴范围

javascript - Angular 2+ 从 http 请求返回变量或映射

jquery - 使用 jquery 取消选中单选按钮列表

c# - 首先在 EF4.1 代码中,使用注释、配置文件或在 OnModelCreating 中配置实体有什么区别?

asp.net - ASP Core Azure Active Directory - 获取名字和姓氏

javascript - 检测事件捕获支持javascript

javascript - 通过 element.style.background 设置时,浏览器会自动将十六进制或 hsl 颜色评估为 rgb?