我的网页包含带有许多输入字段的 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/