话不多说直接上代码
封装一个方法: serializeObject() ——返回对象
版本一、
$.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;
};
版本二、
$.fn.serializeObject = function () {
var ct = this.serializeArray();
var obj = {};
$.each(ct, function () {
if (obj[this.name] !== undefined) {
if (!obj[this.name].push) {
obj[this.name] = [obj[this.name]];
}
obj[this.name].push(this.value || "");
} else {
obj[this.name] = this.value || "";
}
});
return obj;
};