jquery扩展方法(表单数据格式化为json对象)

jquery扩展方法(表单数据格式化为json对象)

<script type="text/javascript">
// 将表单数据序列化为一个json对象,例如 {"name":"zs", "age":10}
// 使用:var jsonObj = $("#formId").serializeObject();
$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  
</script>

猜你喜欢

转载自www.cnblogs.com/xy-ouyang/p/10854383.html