編輯表單後離開本頁面時做提示(jQuery版) 關鍵字: onbeforeunload 表單 提示 添加如下JavaScript: Java代碼 $.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; } Java代碼 $.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; } $.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是否更改過並給出提示。 需要在具體某個頁面裏添加如下代碼來綁定表單原始值: Java代碼 $().ready(function() { $('form').enable_changed_form_confirm(); });
編輯表單後離開本頁面時做提示(jQuery版)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
芯片產業管理和營銷指北(4)—— 產品線經理的修行
zer0black
2024-06-10 14:36:43
【簡寫Mybatis-02】註冊機的實現以及SqlSession處理
xbhog
2024-06-10 14:35:12
kube-vip高可用k8s羣
hiningrise
2024-06-10 14:29:12
Mac 安裝 Node Error: Could not symlink include/node/common.gypi
根據提示,賦權限,刪除文件等 jimmy@MacBook-Pro ~ % brew link node Linking /usr/local/Cellar/node/20.0.0... Er
阿 軍
2024-06-10 14:17:51
EDGE瀏覽器新用戶配置登錄Microsoft賬戶出現0x80190001錯誤代碼
-帥
2024-06-10 14:13:51
hosts文件丟失或更改後如何還原
-帥
2024-06-10 14:13:51
python gdal 安裝使用(Windows, python 3.6.8)
emanlee
2024-06-10 14:13:11
研發高階能力之「技術規劃」
kidney
2024-06-10 14:11:51
Vue TypeScript 實戰:掌握靜態類型編程
Amd794
2024-06-10 14:05:40
Python: two dimensional array
# 假設puzzle是一個包含多個字符串的列表,每個字符串都是同一長度的,代表字母網格的一行 puzzle = [ "JGJGDDAOYD", "IDGFHSPOSA", "FGDIOSAFSC
®Geovin Du Dream Park™
2024-06-10 14:03:20
從零手寫實現 nginx-13-nginx.conf 配置例子解釋 + nginx 配置文件要如何解析?
葉止水
2024-06-10 14:02:20
【MySQL】查詢以A字符開頭以B字符結尾的數據
翎野
2024-06-10 13:52:19
Web Page Marker Pen All In One
xgqfrms
2024-06-10 13:51:29