我有以下代码要处理,以在未保存表单数据的情况下提示用户,但如果单击提交按钮,此代码也会显示警报
$(document).ready(function () {
var _isDirty = false;
$(":input").live("change", function () {
_isDirty = true;
});
$(':[type=submit]').live('click', function () {
_isDirty = false;
});
window.onbeforeunload = function () {
if (_isDirty) {
return 'You have made changes to data on this page. If you navigate away from this page without first saving your data, the changes will be lost.';
}
else {
return null;
}
}
}
onbeforeunload
有一个怪癖,如果您返回任何内容,即使是null,它也会抛出警报。尝试删除else子句else {return null;}
,它应该停止显示。
请关注本文获取答案。