无法将成功标志添加到 onbeforeunload

发布于 2024-09-06 09:27:54 字数 527 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-09-13 09:27:54

问题在于 onclick 事件处理程序不会在 if(!okToSubmit) 之前调用。 您所做的一切

document.getElementById('Register').onclick = function() { okToSubmit = true; };

当您说:只是设置事件处理程序时, 。您实际上并未检索 okToSubmit 的值。

解决此问题的一种方法可能是在注册 onbeforeunload 之前设置 onclick 事件处理程序。

The problem is that the onclick event handler will not be called prior to if(!okToSubmit). All you are doing when you say:

document.getElementById('Register').onclick = function() { okToSubmit = true; };

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 registering onbeforeunload.

冷了相思 2024-09-13 09:27:54

普通的旧 JS 语法有点生疏,所以如果有人需要它,它就在 jQuery 中,这是有效的,至少对于表单提交按钮来说是这样。更改方法以获取是否适合您的需求

<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>

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文