如何填写表单时显示unbeforeunload警报?

时间:2016-01-06 04:14:34

标签: javascript jquery

我想展示" onbeforeunload提醒"选项卡关闭但填写表单后我关闭选项卡" onbeforeunload alert"没有出现。

如何更改代码以及#34; onbeforeunload警告"表格填写时可以显示? 我使用的代码是:

window.onbeforeunload = confirmExit;

function confirmExit() {
  return "You are about to exit the system before freezing your declaration!     If you leave now and never return to freeze your declaration; then they will not     go into effect and you may lose tax deduction, Are you sure you want to leave     now?";
}
$(function() {
  $("a").click(function() {
    window.onbeforeunload = null;
  });
  $("input").click(function() {
    window.onbeforeunload = null;
  });
});

1 个答案:

答案 0 :(得分:1)

  

如何更改代码" unbeforeunload alert"表格填写时可以显示吗?

您实际上是通过填写表单来点击<input />吗?因此,点击<input />后,unbeforeunload将设置为null(删除提醒),代码如下:

$("input").click(function() {
  window.onbeforeunload = null;
});

请删除该代码,然后就可以了。 :)或者您可以定位提交按钮,更具体地说:

$("input").click(function() {
  window.onbeforeunload = null;
});
$("input[type='submit']").click(function() {
  window.onbeforeunload = confirmExit;
});