javascript - 如何在填写表单时显示 onbeforeunload 警报?

标签 javascript jquery

我想在选项卡关闭时显示“onbeforeunload 警报”,但是当我填写表单然后关闭选项卡时,“onbeforeunload 警报”不会出现。

如何更改代码,以便在填写表单时显示“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;
  });
});

最佳答案

How can I change the code so "unbeforeunload alert" can show when the form is filled?

您实际上点击的是<input />通过填写表格吗?因此,一旦您单击 <input />unbeforeunload设置为 null (删除警报)通过以下代码:

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

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

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

关于javascript - 如何在填写表单时显示 onbeforeunload 警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34625529/

相关文章:

javascript - 使用 http.get node.js 的 promise

javascript - 如何在每次服务调用之前运行一个方法?(Angular 7)

jquery - 悬停并使 Accordion 事件暗淡图标

javascript - 更改 Bootstrap 下拉列表时将发布数据发送到 Controller

javascript - 使用 jquery 匹配更改时的输入值

javascript - 我在 div 中有一个长表,当我滚动表时,我想看到它惯性移动 HTML

javascript - jquery定时器实现

jquery - 将 livequery 转换为 live/delegate/?

javascript - Jquery 根据浏览器大小/调整大小更改高度

javascript - .click() 不再工作 jquery