虽然用GET传数组是有点粗暴,最近碰上了多选option筛选传值的问题,用POST不太好。
希望在回到界面的时候能够从url里把筛选状态还原回来,于是需要js获取数组。
GET方式的数组形式是:xxx?array[]=value1&array[]=value2…
把之前在网上找的GetRequest,改了一下支持读取数组值。
function GetRequest() { var url = location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { key_value = strs[i].split("=").map(decodeURI).map(unescape); if (key_value[0].indexOf('[') != -1) { key = key_value[0].match(/(\S*)\[\]/)[1]; (theRequest[key] = theRequest[key] || new Array()).push(key_value[1]); } else { theRequest[key_value[0]] = key_value[1]; } } } return theRequest; }
测试:
> GetRequest() < {a: "2==1", b: ["1[3]@&^嘻嘻@#", "1W、\\=F@哈哈&%*^[]./"]}