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