$.fn.serializeObject = function () {
var obj = {};
var count = 0;
$.each(this.serializeArray(), function (i, o) {
var n = o.name, v = o.value;
count++;
obj[n] = obj[n] === undefined ? v
: $.isArray(obj[n]) ? obj[n].concat(v)
: [obj[n], v];
});
//obj.nameCounts = count + "";//表单name个数
return JSON.stringify(obj);
};
调用示例:var serStr = $('#form1').serializeObject();
上面的插件,不能适用于有多个值的输入控件,例如复选框、多选的select。下面,我将插件做进一步的修改,让其支持多选。代码如下:
(function($){
$.fn.serializeJson=function(){
var serializeObj={};
var array=this.serializeArray();
var str=this.serialize();
$(array).each(function(){
if(serializeObj[this.name]){
if($.isArray(serializeObj[this.name])){
serializeObj[this.name].push(this.value);
}else{
serializeObj[this.name]=[serializeObj[this.name],this.value];
}
}else{
serializeObj[this.name]=this.value;
}
});
return serializeObj;
};
})(jQuery);