无法将成功标志添加到 onbeforeunload
onbeforeunload 有问题。我有一个通过 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>
“注册”是提交按钮 ID。请帮忙!
having issues with onbeforeunload. I have a long form broken into segments via a jquery wizard plug in. I need to pop a confirm dialog if you hit back, refresh, close etc on any step but need it to NOT POP the confirm dialog on click of the submit button. had it working, or at least I thought, it doesn't now.
<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' is the submit button ID. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
onclick
事件处理程序不会在if(!okToSubmit)
之前调用。 您所做的一切当您说:只是设置事件处理程序时, 。您实际上并未检索 okToSubmit 的值。
解决此问题的一种方法可能是在注册
onbeforeunload
之前设置onclick
事件处理程序。The problem is that the
onclick
event handler will not be called prior toif(!okToSubmit)
. All you are doing when you say:Is just setting up the event handler. You are not actually retrieving the value of
okToSubmit
.One way to fix this might be to setup the
onclick
event handler before registeringonbeforeunload
.普通的旧 JS 语法有点生疏,所以如果有人需要它,它就在 jQuery 中,这是有效的,至少对于表单提交按钮来说是这样。更改方法以获取是否适合您的需求
Plain old JS syntax a little rusty, so here it is in jQuery if anyone ever needs it, this works, at least for a form submit button. Change method to get if it suits your needs