form表单的数据 转json

form表单的数据 转json

将form表单中的数据序列化数组后转换为Json

一个jquery的扩展方法(网上摘的):

$.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;   
};    

调用

var para = $('form').serializeObject() ;  
para = JSON.stringify(para) ; //para是json字符串
posted @ 2020-09-23 16:12  时间无岸  阅读(767)  评论(0)    收藏  举报