一只特立独行的猪 一只特立独行的🐷
一只特立独行的🐷
  • 主页
  • web开发
  • 博客笔记
  • 软件工具
  • 随笔杂谈
  • 文章归档
  • 影音欣赏
  • 我的资源
  • 关于我
  • wechat_fill

jquery扩展表单formData获取json对象

  • 2017-07-07
  • Web开发
  • 评论(3)

jquery封装好的直接获取表单数据$('#form').serialize()确实很好用,但是往往前端数据可能需要加工才会选择提交给服务端,而且通常会选择stringify的json字符串,但是拿到的formdata不好直接操作,所以这里我找到一种直接可以从form中获取json对象的方法。记一下
$.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;
};

Powered by Wordpress, Theme by Wing-magic
©2023 一只特立独行的🐷 All rights reserved
陕ICP备15006707号-5