在卸载之前遇到问题。我有一个通过jquery向导插件分成多个部分的长表单。我需要弹出一个确认对话框,如果你在任何步骤上点击后退,刷新,关闭等,但需要它不弹出确认对话框点击提交按钮。它工作了,或者至少我认为,它现在不工作了。
<script type="text/javascript">
var okToSubmit = false;
window.onbeforeunload = function() {
document.getElementById('Register').onclick = function() { okToSubmit = true; };
if(!okToSubmit) return "Using the browsers back button will cause you to lose all form data. Please use the Next and Back buttons on the form";
};
</script>‘'Register’是提交按钮ID,请帮助!
发布于 2010-06-17 01:08:35
问题是在if(!okToSubmit)之前不会调用onclick事件处理程序。你所做的就是当你说:
document.getElementById('Register').onclick = function() { okToSubmit = true; };正在设置事件处理程序。您实际上并没有检索到okToSubmit的值。
解决此问题的一种方法可能是在注册onbeforeunload之前设置onclick事件处理程序。
发布于 2010-06-17 03:09:23
普通的旧JS语法有点生疏了,所以这里是用jQuery编写的,如果有人需要它的话,它是有效的,至少对于表单提交按钮是有效的。将方法更改为get,如果它适合您的需要
<script type="text/javascript">
$(document).ready(function(){
var action_is_post = false;
$("form").submit(function () {
action_is_post = true;
});
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!action_is_post)
return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
}
});
</script>https://stackoverflow.com/questions/3055522
复制相似问题