编辑表单后离开本页面时做提示(jQuery版)

关键字: onbeforeunload 表单 提示

添加如下JavaScript:
Java代码 复制代码
  1. $.fn.enable_changed_form_confirm = function () {   
  2.     var _f = this;   
  3.     $('input[@type=text]'this).each(function() {   
  4.         $(this).attr('_value', $(this).val());   
  5.     });   
  6.     $('input[@type=password]'this).each(function() {   
  7.         $(this).attr('_value', $(this).val());   
  8.     });   
  9.     $('textarea'this).each(function() {   
  10.         $(this).attr('_value', $(this).val());   
  11.     });   
  12.     $('input[@type=checkbox]'this).each(function() {   
  13.         var _v = '';   
  14.         if(this.checked == true) {   
  15.             _v = 'on';   
  16.         } else {   
  17.             _v = 'off';   
  18.         }   
  19.         $(this).attr('_value', _v);   
  20.     });   
  21.     $('input[@type=radio]'this).each(function() {   
  22.         var _v = '';   
  23.         if(this.checked == true) {   
  24.             _v = 'on';   
  25.         } else {   
  26.             _v = 'off';   
  27.         }   
  28.         $(this).attr('_value', _v);   
  29.     });   
  30.     $('select'this).each(function() {   
  31.         $(this).attr('_value'this.options[this.selectedIndex].value);   
  32.     });   
  33.        
  34.     $(this).submit(function() {   
  35.         window.onbeforeunload = null;   
  36.     });   
  37.     window.onbeforeunload = function() {   
  38.         if(is_form_changed(_f)) {   
  39.             return "You will lose any unsaved content.";   
  40.         }   
  41.     }   
  42. }   
  43.   
  44. function is_form_changed(f) {   
  45.     var changed = false;   
  46.     $('input[@type=text]', f).each(function() {   
  47.         var _v = $(this).attr('_value');   
  48.         if(typeof($(this).attr('_value')) == 'undefined') {   
  49.             _v = '';   
  50.         }   
  51.         if(_v != $(this).val()) {   
  52.             changed = true;   
  53.         }   
  54.     });   
  55.     $('input[@type=password]', f).each(function() {   
  56.         var _v = $(this).attr('_value');   
  57.         if(typeof($(this).attr('_value')) == 'undefined') {   
  58.             _v = '';   
  59.         }   
  60.         if(_v != $(this).val()) {   
  61.             changed = true;   
  62.         }   
  63.     });   
  64.     $('textarea', f).each(function() {   
  65.         var _v = $(this).attr('_value');   
  66.         if(typeof($(this).attr('_value')) == 'undefined') {   
  67.             _v = '';   
  68.         }   
  69.         if(_v != $(this).val()) {   
  70.             changed = true;   
  71.         }   
  72.     });   
  73.     $('input[@type=checkbox]', f).each(function() {   
  74.         var _v = '';   
  75.         if(this.checked == true) {   
  76.             _v = 'on';   
  77.         } else {   
  78.             _v = 'off';   
  79.         }   
  80.         if($(this).attr('_value') != _v) {   
  81.             changed = true;   
  82.         }   
  83.     });   
  84.     $('input[@type=radio]', f).each(function() {   
  85.         var _v = '';   
  86.         if(this.checked == true) {   
  87.             _v = 'on';   
  88.         } else {   
  89.             _v = 'off';   
  90.         }   
  91.         if($(this).attr('_value') != _v) {   
  92.             changed = true;   
  93.         }   
  94.     });   
  95.     $('select', f).each(function() {   
  96.         var _v = $(this).attr('_value');   
  97.         if(typeof($(this).attr('_value')) == 'undefined') {   
  98.             _v = '';   
  99.         }   
  100.         if(_v != this.options[this.selectedIndex].value) {   
  101.             changed = true;   
  102.         }   
  103.     });   
  104.     return changed;   
  105. }  
$.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代码 复制代码
  1. $().ready(function() {   
  2.     $('form').enable_changed_form_confirm();   
  3. });  

你可能感兴趣的:(JavaScript,jquery,F#)