测试页面:
<html>
<head>
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="div1">
<button onclick="test1()">
t1
</button>
<button onclick="test2()">
t2
</button>
</div>
</body>
<script type="text/javascript">
var parseParam = function(param, key) {
var paramStr = "";
if (typeof param == ‘string‘ || typeof param == ‘number‘ || typeof param == ‘boolean‘) {
paramStr += "&" + key + "=" + encodeURIComponent(param);
} else {
$.each(param, function(i,tmp) {
var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
paramStr += ‘&‘ + parseParam(tmp, k);
});
}
return paramStr.substr(1);
};
function test1() {
console.log(parseParam({a:"12",b:123}));
}
function test2() {
console.log(parseParam({a:"12",b:123},"user"));
}
</script>
</html>
测试结果:
a=12&b=123
user.a=12&user.b=123
时间: 2024-10-05 14:49:35