前端表单序列化为json串,以及构造json数组、json串

时间:2024-09-03 15:04:14
var parm={
username:"zhangsan",
age:24,
email:"352400260@qq.com"
};
console.log(JSON.stringify(parm)); var person = new Object();
person.username = "lisi";
person.age = "34";
person.email = "1350812980@qq.com";
console.log(JSON.stringify(person));

  前端表单序列化为json串,以及构造json数组、json串

(function($){
$.fn.serializeJson=function(){
var serializeObj={};
var array=this.serializeArray();
$(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);