如何在javascript中从URL获取值,参数在数组中我该怎么办请帮助我,并提前感谢我的代码和URL如下:
网址是:
propertyList?address=&sub_type%5B%5D=3&sub_type%5B%5D=6&min_price=
我想要sub_type
值,我得到了其他参数值,这些参数值是单一的,而不是使用此函数的数组:
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
我使用了此函数并获得了address
和min_price
值,但它在sub_type
中无效。
如何获得sub_type
值?
答案 0 :(得分:0)
function getUrlParameter(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}
//call method
alert(getUrlParameter("arameter1"));