检测未保存的更改 [英] Detecting Unsaved Changes

查看:163
本文介绍了检测未保存的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现在ASP .NET应用程序的未保存更改提示的要求。如果用户修改Web表单上的控件,并尝试导航离开保存之前,提示应该出现警告他们,他们有未保存的更改,并给他们取消并停留在当前页面上的选项。如果用户还没有触摸任何控件的提示应不显示

I have a requirement to implement an "Unsaved Changes" prompt in an ASP .Net application. If a user modifies controls on a web form, and attempts to navigate away before saving, a prompt should appear warning them that they have unsaved changes, and give them the option to cancel and stay on the current page. The prompt should not display if the user hasn't touched any of the controls.

在理想情况下,我想在JavaScript来实现这一点,但在此之前我下去我自己的滚动code的路径,是否有实现这一目标的任何现有的框架或推荐的设计模式?理想情况下,我想的东西,可以很容易地跨越多个页面进行很小的改动重用。

Ideally I'd like to implement this in JavaScript, but before I go down the path of rolling my own code, are there any existing frameworks or recommended design patterns for achieving this? Ideally I'd like something that can easily be reused across multiple pages with minimal changes.

推荐答案

使用jQuery:

var _isDirty = false;
$("input[type='text']").change(function(){
  _isDirty = true;
});
// replicate for other input types and selects

onunload的 / onbeforeunload 方法相结合的要求。

Combine with onunload/onbeforeunload methods as required.

从意见,以下引用的所有输入字段,而无需复制code:

From the comments, the following references all input fields, without duplicating code:

$(':input').change(function () {

使用 $(:输入)。指的是所有输入,文本区域,选择和按钮元素

Using $(":input") refers to all input, textarea, select, and button elements.

这篇关于检测未保存的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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