使用Jquery或JS在具有相同名称的多个参数时获取URL查询字符串参数 [英] Get URL Query String Parameters when there are multiple parameters with the same name using Jquery or JS
问题描述
我有一个由多个过滤器组成的URL,这些过滤器被命名并具有相应的搜索条件,我无法更改这些URL的构造方式.
I have a URL that is comprised of multiple filters that are named and have corresponding search criteria, I cannot change the way these urls are constructed.
我知道在仅尝试获取一个参数值的情况下如何获取查询字符串参数,但是我似乎无法弄清楚如何将它们适当地关联在一起.例如,这里是网址:
I am aware of how to get query string parameters in a scenario where I am just trying to get the value of one parameter, however I can't seem to figure out how to associate them together appropriately. For example here is the url:
/AllItems.aspx?FilterName=FilterA&FilterMultiValue=*FilterASearchValue*&FilterName=FilterB&FilterMultiValue=*FilterBSearchValue*"
我想从该URL中获取值,以便可以将Filter A与FilterA搜索值相关联,并将Filter B与FilterB搜索值相关联
I would like to get the values from this url so that I can associate Filter A with the FilterA Search Value and Associate Filter B with the FilterB Search Value
推荐答案
您可以使用reduce来做到这一点:
you can use reduce to do that:
var u='/AllItems.aspx?FilterName=FilterA&FilterMultiValue=*FilterASearchValue*&FilterName=FilterB&FilterMultiValue=*FilterBSearchValue*'
u.split(/[?&]/).reduce(function(a,b,c){
var p=b.split("="), k=p[0], v=decodeURIComponent(p[1]);
if(!p[1])return a;
a[k]=a[k]||[];
a[k].push(v);
return a;
}, {})
返回参数数组而不是字符串,从而允许重复使用相同名称的参数:
which returns an array of params instead of a string, allowing same-name params to repeat:
{
"FilterName": [
"FilterA",
"FilterB"
],
"FilterMultiValue": [
"*FilterASearchValue*",
"*FilterBSearchValue*"
]
}
这篇关于使用Jquery或JS在具有相同名称的多个参数时获取URL查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!