$.fn.serializeObject = function () {
var keyValues = this.serializeArray();
var pattern=/[(d+)]/;
var filter_keys = [],
normal_keys = keyValues.filter(function (v, i) {
if (~v.name.indexOf('.')||~pattern.test(v.name)) {
filter_keys.push(v);
return false
}
return true;
})
var resultJson = {};
filter_keys.forEach(function (v, i, ary) {
var v_ary = v.name.split('.'),
v_last = v_ary.length - 1;
var deal = function (obj, j, array) {
var me = arguments.callee,
islast = j === v_last ? true : false;
if (islast&&!pattern.test(array[j])) {
if(typeof obj =='array'){
return obj.push(v.value);
}else{
if(obj[array[j]]){
obj[array[j]]+=','+v.value||'';
}else{
obj[array[j]] = v.value || '';
}
return;
}
}else if(islast&&pattern.test(array[j])){
var name = array[j].substr(0, array[j].indexOf('['));
var index = array[j].match(pattern)[1];
if (!obj[name]) {
obj[name] = [];
}
if (obj[name].length<= index) {
return obj[name].push(v.value||'');
}
}
var nextObj;
if (pattern.test(array[j])) {
var name = array[j].substr(0, array[j].indexOf('['));
var index = array[j].match(pattern)[1];
if (!obj[name]) {
obj[name] = [];
}
if (obj[name].length<= index) {
nextObj={};
obj[name].push(nextObj);
}else{
nextObj=obj[name][index];
}
} else {
if (!obj[array[j]]) {
obj[array[j]] = {};
}
nextObj=obj[array[j]];
}
return me(nextObj, j + 1, array);
}
deal(resultJson, 0, v_ary);
});
normal_keys.forEach(function (v) {
resultJson[v.name] = v.value;
});
return resultJson;
}
1.表单input等name命名如下book[0].title
2.支持多级数组
3.表单数组下标必须按照顺序并从0开始
4.有相同name标签将合并多个name的值并以“,”分割