将表单数据转成json对象

var jsonObj = $("#form1").serializeObject(); //json对象
console.log(jsonObj)
var jsonStr = JSON.stringify($("#form1").serializeObject()); //json字符串
console.log(jsonStr)

$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
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;
};

 

posted on 2020-07-29 09:49  追星程序媛  阅读(34)  评论(0编辑  收藏  举报