- 浏览: 2650077 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
80后的童年2:
深入浅出MongoDB应用实战开发网盘地址:https://p ...
MongoDB入门教程 -
shliujing:
楼主在不是精通java和php的前提下,请不要妄下结论。
PHP、CakePHP哪凉快哪呆着去 -
安静听歌:
希望可以一给一点点注释
MySQL存储过程之代码块、条件控制、迭代 -
qq287767957:
PHP是全宇宙最强的语言!
PHP、CakePHP哪凉快哪呆着去 -
rryymmoK:
深入浅出MongoDB应用实战开发百度网盘下载:链接:http ...
MongoDB入门教程
添加如下JavaScript:
上面的代码将表单的原始值以一个名为"_value"的attr保持在input和select标签上
window.onbeforeunload时判断form是否更改过并给出提示。
需要在具体某个页面里添加如下代码来绑定表单原始值:
$.fn.enable_changed_form_confirm = function () { var _f = this; $('input[@type=text]', this).each(function() { $(this).attr('_value', $(this).val()); }); $('input[@type=password]', this).each(function() { $(this).attr('_value', $(this).val()); }); $('textarea', this).each(function() { $(this).attr('_value', $(this).val()); }); $('input[@type=checkbox]', this).each(function() { var _v = ''; if(this.checked == true) { _v = 'on'; } else { _v = 'off'; } $(this).attr('_value', _v); }); $('input[@type=radio]', this).each(function() { var _v = ''; if(this.checked == true) { _v = 'on'; } else { _v = 'off'; } $(this).attr('_value', _v); }); $('select', this).each(function() { $(this).attr('_value', this.options[this.selectedIndex].value); }); $(this).submit(function() { window.onbeforeunload = null; }); window.onbeforeunload = function() { if(is_form_changed(_f)) { return "You will lose any unsaved content."; } } } function is_form_changed(f) { var changed = false; $('input[@type=text]', f).each(function() { var _v = $(this).attr('_value'); if(typeof($(this).attr('_value')) == 'undefined') { _v = ''; } if(_v != $(this).val()) { changed = true; } }); $('input[@type=password]', f).each(function() { var _v = $(this).attr('_value'); if(typeof($(this).attr('_value')) == 'undefined') { _v = ''; } if(_v != $(this).val()) { changed = true; } }); $('textarea', f).each(function() { var _v = $(this).attr('_value'); if(typeof($(this).attr('_value')) == 'undefined') { _v = ''; } if(_v != $(this).val()) { changed = true; } }); $('input[@type=checkbox]', f).each(function() { var _v = ''; if(this.checked == true) { _v = 'on'; } else { _v = 'off'; } if($(this).attr('_value') != _v) { changed = true; } }); $('input[@type=radio]', f).each(function() { var _v = ''; if(this.checked == true) { _v = 'on'; } else { _v = 'off'; } if($(this).attr('_value') != _v) { changed = true; } }); $('select', f).each(function() { var _v = $(this).attr('_value'); if(typeof($(this).attr('_value')) == 'undefined') { _v = ''; } if(_v != this.options[this.selectedIndex].value) { changed = true; } }); return changed; }
上面的代码将表单的原始值以一个名为"_value"的attr保持在input和select标签上
window.onbeforeunload时判断form是否更改过并给出提示。
需要在具体某个页面里添加如下代码来绑定表单原始值:
$().ready(function() { $('form').enable_changed_form_confirm(); });
评论
4 楼
bosschen
2013-11-05
试试 看能不能用
3 楼
birdjavaeye
2008-07-21
还是太复杂。当时在CMS上用得方法是,页面onload时,用jquery的一个叫啥子的函数(忘了)把form编码成字符串,存在一个变量里。然后在 onbeforeunload中再次编码,判断不等,就提示。
2 楼
hideto
2007-11-17
nice, thx!
1 楼
jctr
2007-11-16
jQuery支持多个DOM一起选择,不介意我帮您做了简单的修改,但愿意没变,其实我一直关注您在RoR方面的文章。
$.fn.enable_changed_form_confirm = function () { var _f = this; $(':text, :password, textarea', this).each(function() { $(this).attr('_value', $(this).val()); }); $(':checkbox, :radio', this).each(function() { var _v = this.checked ? 'on' : 'off'; $(this).attr('_value', _v); }); $('select', this).each(function() { $(this).attr('_value', this.options[this.selectedIndex].value); }); $(this).submit(function() { window.onbeforeunload = null; }); window.onbeforeunload = function() { if(is_form_changed(_f)) { return "You will lose any unsaved content."; } } } function is_form_changed(f) { var changed = false; $(':text, :password, textarea', f).each(function() { var _v = $(this).attr('_value'); if(typeof(_v) == 'undefined') _v = ''; if(_v != $(this).val()) changed = true; }); $(':checkbox, :radio', f).each(function() { var _v = this.checked ? 'on' : 'off'; if(_v != $(this).attr('_value')) changed = true; }); $('select', f).each(function() { var _v = $(this).attr('_value'); if(typeof(_v) == 'undefined') _v = ''; if(_v != this.options[this.selectedIndex].value) changed = true; }); return changed; } $(function() { $('form').enable_changed_form_confirm(); });
发表评论
-
Ext源码解析:3, DomHelper.js
2008-07-15 16:45 2375from http://www.beyondrails.com ... -
Ext源码解析:2, DomQuery.js
2008-07-11 10:54 2546fromhttp://www.beyondrails.com/ ... -
Ext源码解析:1, Ext.js
2008-07-09 18:08 2887来自http://www.beyondrails.com/bl ... -
Extjs Introduction
2008-07-08 02:04 8768from http://hideto.beyondrails. ... -
模拟Ajax提交上传文件
2008-06-04 00:24 4184XMLHTTP不支持文件上传这种form提交,但是我们可以模拟 ... -
escape JavaScript
2008-03-27 16:55 2600单引号、双引号、<script></scri ... -
Multiple IE
2007-11-22 10:35 2499老问题,js和css对跨浏览器兼容问题 在一台电脑上共存IE3 ... -
正确使用Prototype,节省额外的100K
2007-11-10 23:20 3059Part I: http://thinkweb2.com/pr ... -
十大Web应用漏洞清单,XSS排名第一
2007-10-22 12:36 3050owasp.org列出十大Web应用漏洞清单: 1, Cros ... -
IE下不能disabled掉select标签的option的解决方案
2007-10-11 17:48 8969原文:Select, Option, Disabled And ... -
Jester: JavaScript Client for REST
2007-09-04 13:51 2680Jester: JavaScriptian REST介绍了Je ... -
ASCB阅读笔记五、Arrays
2007-08-23 10:47 1788var array:Array = new Array() ... -
ASCB阅读笔记四、Numbers and Math
2007-08-15 12:08 1951显示最近的整数(四舍五入) Math.round(204.49 ... -
ASCB阅读笔记三、Runtime Environment
2007-08-10 23:34 24801,检测用户浏览器安装的Flash Player版本 http ... -
ASCB阅读笔记二、Custom Classes
2007-08-09 10:54 13411,ActionScript 3.0已经完全OO,所有AS代码 ... -
ASCB阅读笔记一、ActionScript Basics
2007-08-07 23:29 20101,使用trace来debug程序 package { ... -
method_missing in ActionScript 3/Flex
2007-08-07 18:05 1954method_missing in ActionScript ... -
Hilog 0.1 released.
2007-08-07 00:52 2050Hilog 0.1 release is a demo of ... -
在客户端保存状态
2007-08-05 18:13 3708Keeping State on the Client 在第 ... -
介绍Cairngorm
2007-08-05 15:36 19680Cairngorm是Adobe Labs上的Flex MVC框 ...
相关推荐
最近做一个表单弹出框,表单保存提交,但是,产品提出,用户不保存,而关闭弹出框时,要给出一个弹窗提示。这个功能,可以用watch监听表单数据。当数据表单发生变化,用户点击了关闭按钮,则根据监听结果来判断用户...
表单修改后,弹出对话框询问离开本页前提示是否离开
jquery表单验证代码百度注册页面表单验证
代码简介:jQuery可视化表单拖拽编辑代码是一款基于jQuery和bootstrap框架制作可视化表单,可自定义拖拽控制生成表单,预览表单,表单各种属性可通过json格式的配置来处理控制表单的各个属性,看起来非常的高大上,...
jQuery表单验证jQuery表单验证jQuery表单验证jQuery表单验证jQuery表单验证jQuery表单验证jQuery表单验证
jQuery填写表单带步骤指引的表单向导提示插件 jQuery填写表单带步骤指引的表单向导提示插件
jQuery表单消息提示功能
jquery带提示验证表单
不错的表单提示插件jquery-tooltip,jquery-tooltip jquery插件 表单提示工具
比较全面的讲解了jquery获取表单值,全面的讲解了jquery获取表单值
jquery表单验证插件
jquery 表单验证大全并以弹出形式提示信息
jQuery formValidator表单验证插件,它是基于jQuery类库,实现了js脚本于页面html代码的分离。你可以划分多个校验组,每个组的校验都是互不影响。对一个表单对象,你只需要写一行代码就可以轻松实现无数种(理论上)...
jQuery表单验证,我们常见的互联网注册时即时弹出提示情况,非常有用
jQuery-Validation-Engine-master 表单验证插件 简单好用,已经包含汉化
自做的一个简单的表单验证 jquery validate 表单验证 jquery validate 表单验证
jQuery表单验证插件EasyValidator 2.0带TIP提示效果
jQuery表单提交验证verify一套完整的用户注册前端校验,包含用户名,密码强度,显示隐藏密码,手机号输入控制手机验证码,真实姓名,身份证号等验证。
一个关于用户注册的JQuery表单验证小项目。