【发布时间】:2017-05-29 15:40:19
【问题描述】:
一个可能没有结束的问题是如何从 URL 中获取变量。在我的所有搜索中,我发现了几种从 url 获取 A=aValue 的非常好的方法。
但我的问题是 我需要
?Company=FLHS&Device=Crosstown_PCC_01&A=aValue&A=secondAValue
我需要 url 中两个 A 的数组,我需要知道 aValue 是第一个,secondAValue 是第二个
我有 jquery Mobile。
更新
这就是我现在所拥有的
var urlParamDevice = getURLParameter('DeviceID');
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
);
getURLParameter(name) 需要更健壮一些。
更新 2,2014/03/07 这是我从建议的答案中得出的结论
function getQueryParams(name) {
qs = location.search;
var params = [];
var tokens;
var re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs))
{
if (decodeURIComponent(tokens[1]) == name)
params.push(decodeURIComponent(tokens[2]));
}
return params;
}
【问题讨论】:
-
这里有将数组(或任何其他对象)作为参数传递的示例:api.jquery.com/jQuery.param
-
谢谢,试图从 URL 中获取一个数组,但没有通过。
标签: javascript jquery