function serialize(target) {
let arrFormData = [], objFormData = {},rel = {};
[].slice.call(target.elements).forEach(function (ele) {
let type = ele.type,disabled = ele.disabled,name = ele.name,value = encodeURIComponent(ele.value || '');
if(!name || disabled || !type ||(/^reset|submit|image$/i.test(type)) || (/^checkbox|radio$/i.test(type) && !ele.checked)){
return;
}
type = type.toLocaleLowerCase();
if(type !== 'select-multiple') {
if (objFormData[name]) {
objFormData[name].push(value);
} else {
objFormData[name] = [value];
}
}else{
[].slice.call(ele.querySelector('option')).forEach(function (option) {
let optionValue = encodeURIComponent(option.value || '');
if(option.selected){
if(objFormData[name]){
objFormData[name].push(optionValue);
}else{
objFormData[name] = [optionValue];
}
}
});
}
});
for(let key in objFormData){
arrFormData.push(key+'='+objFormData[key].join());
}
// return arrFormData.join('&');
return objFormData;
}
複製代碼
能夠返回json格式數據,也能夠返回拼接好的字符串 在最後return的時候能夠選擇返回格式 用的時候把最後改一下就好了json