jQuery的阅读从几个URL参数数组 [英] Jquery reading several array parameters from url

查看:108
本文介绍了jQuery的阅读从几个URL参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址:

<$p$p><$c$c>http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa

该网址包含了一个数组发送paramters的很多:

现在我希望让每个阵列和它的价值了。

在上面的例子中,结果应该是这样的:

  searchValue =阵列(
[0] ='耐克网络商店'
[1] ='Bodyman'
);category_filter =阵列(
[0] ='动物和放大器;宠物用品
[1] ='时尚'
);country_filter =阵列(
[0] ='奥兰群岛
[1] ='美属萨摩亚
);

有没有可能把它弄出来这样的如果又如何?我试图用下面的:

 德codeURIComponent(
    (正则表达式(名称+'='+'(+)(安培;?| $)')EXEC(location.search)|| [,空])[1]

然而,这只是返回值1(耐克网络商店)在我的例子。


解决方案

作为参数数组。下面code将工作得很好。

  //我们的测试网址
VAR URL =\"http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa\" ;
//过滤字符串..
变种paramsList = url.slice(url.indexOf()+ 1,url.length?);
变种filteredList = paramsList.split(与&amp;);//一个对象存储阵列
变种objArr = {};//下面的循环是很明显......我们只是删除[]和+ ..和分裂成一对键和值..和存储作为一个数组...
为(变量I = 0,L = filteredList.length; I&所述; l;和I + = 1){
  VAR参数=去codeURIComponent(filteredList [I] .replace([],))取代(/ \\ + /克,);
  变种对= param.split(=);
  如果{objArr [对[0]] = [];}(objArr [对[0]!)
  objArr [对[0]推(对[1])。
}的console.log(objArr);

这将给我们....

  [对象的对象] {
  category_filter:动物和放大器;宠物用品,时尚]
  country_filter:奥兰群岛,美国萨摩亚],
  sea​​rchValue:耐克网络商店,Bodyman]
}

希望这有助于..:D

I have the following URL:

http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa

This url contains alot of paramters that are sent as an array:

Now i wish to get each individual array and its value out

in the above example the result should be something like this:

searchValue = array(
[0] = 'Nike Webstore'
[1] = 'Bodyman'
);

category_filter = array(
[0] = 'Animals & Pet Supplies'
[1] = 'Fashion'
);

country_filter = array(
[0] = 'Aland Islands'
[1] = 'American Samoa'
);

is it possible to get it out like this and if so how? i have attempted with the following:

 decodeURIComponent(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]

However this only returned 1 value (Nike Webstore) in my example.

解决方案

as parameters are an array. the below code will work just fine..

// our test url
var url ="http://mydomain/Forwards?searchValue[]=Nike+Webstore&searchValue[]=Bodyman&category_filter[]=Animals+%26+Pet+Supplies&category_filter[]=Fashion&country_filter[]=Aland+Islands&country_filter[]=American+Samoa" ;
// filtering the string..
var paramsList = url.slice(url.indexOf("?")+1,url.length) ;
var filteredList =  paramsList.split("&") ;

// an object to store arrays
var objArr = {} ;

// the below loop is obvious... we just remove the [] and +.. and split into pair of key and value.. and store as an array...
for (var i=0, l=filteredList.length; i <l; i +=1 ) {
  var param = decodeURIComponent(filteredList[i].replace("[]","")).replace(/\+/g," ") ;
  var pair = param.split("=") ;
  if(!objArr[pair[0]]) {  objArr[pair[0]] = [] ;}
  objArr[pair[0]].push(pair[1]);
}

console.log(objArr);

which will give us....

[object Object] {
  category_filter: ["Animals & Pet Supplies", "Fashion"],
  country_filter: ["Aland Islands", "American Samoa"],
  searchValue: ["Nike Webstore", "Bodyman"]
}

hope this helps.. :D

这篇关于jQuery的阅读从几个URL参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆