如何在javascript中从URL获取数组参数值

时间:2018-01-10 06:31:28

标签: javascript php

如何在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];
    }
  }
};

我使用了此函数并获得了addressmin_price值,但它在sub_type中无效。

如何获得sub_type值?

1 个答案:

答案 0 :(得分:0)

enter image description here

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"));