jQuery警告如果离开页面而不点击保存按钮 [英] jquery warn if leaving page without clicking the save button

查看:101
本文介绍了jQuery警告如果离开页面而不点击保存按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您更改页面上的数据然后离开页面,以下代码将执行警告。我想要-除了用户按下保存按钮时。

The following code executes a warning if you change data on the page and then leave the page. I want that - except when the user presses the save button.

这是jquery代码:

This is the jquery code:

$(document).ready(function() {
  formmodified=0;
    $('form *').change(function(){
        formmodified=1;
    });
  window.onbeforeunload = confirmExit;
    function confirmExit() {
      if (formmodified == 1) {
          return "New information not saved. Do you wish to leave the page?";
      }
  }
});

这是保存数据的按钮:

<input class="btn-primary" name="commit" type="submit" value="Save Material">

更新
谢谢tymeJV !!!!!!!

UPDATE Thanks tymeJV !!!!!!!

万一有人需要它,它可以工作:

In case someone else need it, this works:

$(document).ready(function() {
    formmodified=0;
    $('form *').change(function(){
        formmodified=1;
    });
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        if (formmodified == 1) {
            return "New information not saved. Do you wish to leave the page?";
        }
    }
    $("input[name='commit']").click(function() {
        formmodified = 0;
    });
});


推荐答案

给出保存按钮的信息:

<input class="btn-primary" name="commit" type="submit" value="Save Material">

您可以设置 formmodified

$("input[name='commit']").click(function() {
    formmodified = 0;
});

您不应该立即触发最后的支票

Your check at the end shouldn't be triggered now

这篇关于jQuery警告如果离开页面而不点击保存按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆