zoukankan      html  css  js  c++  java
  • 使用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.     };  
  • 相关阅读:
    前端常见跨域解决方案
    VS单元测试--初级篇
    高等数学思路
    二元函数求极值判别式AC-B^2
    向量积详解
    伯努利分布均值和方差
    两个标准正态随机变量相乘的方差
    a分位数与双侧a分位数
    中心极限定理概念理解与记忆
    样本方差概念解析
  • 原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/6877078.html
Copyright © 2011-2022 走看看