(function(){
var formContrast = function(form){
var _that = this;
this.initData = {};
this.init = function(){
_that.initData = _that.getFormData();
}
this.getInitData = function(){
return _that.initData;
}
this.getFormData = function(){
var arrElem = form.elements;
var formData = {};
$.each(arrElem,function(i,v){
//console.dir(v);
if(v.type == "radio"){
if (v.checked) {
formData[v.name] = v.value;
};
}else if(v.type == "checkbox"){
if(v.checked){
if(typeof(formData[v.name]) == "undefined"){
formData[v.name] = v.value;
}else{
formData[v.name] += ','+v.value;
}
};
}else if(v.type == "select-multiple"){
var selected = $(v).find(':selected');
var arrValue = [];
$.each(selected,function(i,v){
arrValue.push(v.value);
});
formData[v.name] = arrValue.join(',');
}else{
formData[v.name] = v.value;
}
});
return formData;
}
this.contrast = function(){
var formContrast = true;
var curFormData = _that.getFormData();
$.each(curFormData,function(i,v){
console.log(i,'----',v);
if(_that.initData[i]!=v){
formContrast = false;
};
});
return formContrast;
}
this.init();
}
$.fn.wypFormContrast = function(){
if(this[0] instanceof HTMLFormElement){
return new formContrast(this[0]);
}else{
console.log('不是form对象~');
return;
};
}
})();
表单数据插件,用于获取表单数据,判断表单是否改变
最新推荐文章于 2023-11-03 17:42:21 发布