He元素

Don't be shy just try!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

页面中引用了jquery,第一想到的就是序列化,但是序列化后的表单字段为a=1&b=2这种。

这里写一个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;    
};  

这个方法是将表单序列化成json的。

 

像这样调用:

var para = $('form').serializeObject() ;   
para = JSON.stringify(para) ; 

先把表单数据序列化为一个json对象,然后将json对象转换成一个json字符串。

这样para就是一个json字符串啦。就可以发起请求了

posted on 2016-05-19 21:24  He元素  阅读(6544)  评论(0编辑  收藏  举报