我在this answer中写了一个函数来帮助你。我投票结束这个问题作为这个问题的重复,但现在我看到在这个问题上有额外的要求。当我写这个问题的答案,我做了一个版本,将处理数组风格的URL参数太多:
(function() {
var e,
d = function (s) { return decodeURIComponent(s).replace(/\+/g, " "); },
q = window.location.search.substring(1),
r = /([^&=]+)=?([^&]*)/g;
while (e = r.exec(q)) {
if (e[1].indexOf("[") == "-1")
urlParams[d(e[1])] = d(e[2]);
else {
var b1 = e[1].indexOf("["),
aN = e[1].slice(b1+1, e[1].indexOf("]", b1)),
pN = d(e[1].slice(0, b1));
if (typeof urlParams[pN] != "object")
urlParams[d(pN)] = {},
urlParams[d(pN)].length = 0;
if (aN)
urlParams[d(pN)][d(aN)] = d(e[2]);
else
Array.prototype.push.call(urlParams[d(pN)], d(e[2]));
}
}
})();
你可以看到一个演示在这里工作:http://jsbin.com/adali3/2
样品查询字符串:
test=Hello&person[]=jeff&person[]=jim&person[extra]=john&nocache=1290122355841
结果:
{
"test": "Hello",
"person": {
"0": "jeff",
"1": "jim",
"length": 2,
"extra": "john"
},
"nocache": "1290100673882"
}
重复:HTTP:/ /stackoverflow.com/questions/1131630/javascript-jquery-param-inverse-function – Mark 2010-11-18 17:02:15