/* 来源于博客园http://www.cnblogs.com/nixil/archive/2010/12/08/1900745.html */ function paramString2obj(serializedParams) { var obj = {}; function evalThem(str) { var attributeName = str.split("=")[0]; var attributeValue = str.split("=")[1]; if (!attributeValue) { return; } //debugger; var array = attributeName.split("."); for (var i = 1; i < array.length; i++) { var tmpArray = Array(); tmpArray.push("obj"); for (var j = 0; j < i; j++) { tmpArray.push(array[j]); }; var evalString = tmpArray.join("."); if (!eval(evalString)) { eval(evalString + "={};"); } }; //以下对form表单中的checkbox多选操作进行更新,默认将多选的值以逗号进行分割,避免后台取值时,只取到一个选中的值 var oldValue = eval("obj." + attributeName + ";"); if (oldValue) { eval("obj." + attributeName + "=‘" + oldValue + "," + attributeValue + "‘;") } else { eval("obj." + attributeName + "=‘" + attributeValue + "‘;"); } }; var properties = serializedParams.split("&"); for (var i = 0; i < properties.length; i++) { evalThem(properties[i]); }; return obj; } $.fn.form2json = function () { var serializedParams = this.serialize(); //console.log(serializedParams) var obj = paramString2obj(serializedParams); var str = decodeURIComponent(JSON.stringify(obj)) return $.parseJSON(str); } //$.fn.form2json = function () { // var serializedParams = this.serialize(); // var obj = paramString2obj(serializedParams); // return JSON.stringify(obj); //}
时间: 2024-12-21 16:13:54