将form表单序列化

function form2Json(formID)
{
		var serializeObj = {};
		var aray = $(formID).serializeArray();
		$(aray).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
			{ 
				if(this.name.indexOf(".")>0)//检查是否有嵌套对象
				{
					var names = this.name.split(".");
					var objName = names[0];
					var propertyName = names[1];
					serializeObj[objName] = {};
					serializeObj[objName][propertyName] = this.value;
				}
				else
				{
					serializeObj[this.name] = this.value;
				}
			}
		});
		return JSON.stringify(serializeObj);
}

你可能感兴趣的:(js)