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>

'Register' 是提交按钮 ID。请帮忙!

4

2 回答 2

0

普通的旧 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>
于 2010-06-16T19:09:22.953 回答
0

问题是onclick事件处理程序不会在if(!okToSubmit). 当你说:

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

只是设置事件处理程序。您实际上并没有检索okToSubmit.

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

于 2010-06-16T17:08:35.117 回答