使用jquery将表单自动封装成json对象 /json对象元素的添加删除和转换

  1. $.fn.serializeObject = function () {  
  2.         var o = {};  
  3.         var a = this.serializeArray();  
  4.         $.each(a, function () {  
  5.             if (o[this.name]) {  
  6.                 if (!o[this.name].push) {  
  7.                     o[this.name] = [o[this.name]];  
  8.                 }  
  9.                 o[this.name].push(this.value || '');  
  10.             } else {  
  11.                 o[this.name] = this.value || '';  
  12.             }  
  13.         });  
  14.         return o;  
  15.     };  
  16. var formData = $("#editForm").serializeObject();  

var json={
   id:"id1",
   name:"开心"
};
 
//添加
json["gender"]="男"
json["age"]=26;
alert(json.age);
 
//删除
delete json["age"];
alert(json.age);
alert(json.gender);

  1. $.fn.serializeObject = function () {  
  2.         var o = {};  
  3.         var a = this.serializeArray();  
  4.         $.each(a, function () {  
  5.             if (o[this.name]) {  
  6.                 if (!o[this.name].push) {  
  7.                     o[this.name] = [o[this.name]];  
  8.                 }  
  9.                 o[this.name].push(this.value || '');  
  10.             } else {  
  11.                 o[this.name] = this.value || '';  
  12.             }  
  13.         });  
  14.         return o;  
  15.     };  
原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/6877078.html