ajax多表单序列化

时间:2021-08-19 07:48:03

今天在修一个后台接收参数为空值的bug

找了好久才发现原来是form表单合拼参数的时候把参数给搞没了

(我也不知道为什么啊—_—!)

//对表单进行Json对象序列化
(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);  
    var baseinfo = $("#baseInfo").serializeJson();
    var intension = $("#intension").serializeJson();
    var edu = $("#edu").serializeJson();
    var workExp = $("#workExp").serializeJson();
    var project = $("#project").serializeJson();//alert(JSON.stringify(project));
    var langSkills = $("#langSkills").serializeJson();
    //console.log("这是"+JSON.stringify(langSkills));
    var specialty = $("#specialty").serializeJson();
    var training = $("#training").serializeJson();
    var certs = $("#certs").serializeJson();
    var selfAssessment = $("#selfAssessmentForm").serializeJson();
    var works = $("#show").serializeJson();
    var param = $.extend({},baseinfo,intension,edu,workExp,project,langSkills,specialty,training,certs,selfAssessment,works);
       //不要去掉这些解释,请检查合拼参数的时候是否遗漏了某些值
       //console.log(langSkills.levelId+"hello");
    param.levelId = langSkills.levelId;
   //    console.log(param.langId+"555"+param.levelId);
       //    console.log(JSON.stringify(param));
 

幸亏机智的我想到了这样一种解决方法,就是直接把漏掉的参数赋值给合拼后的参数,虽然不太好,总算解决一个问题吧