如何从JavaScript中的URL获取具有相同名称的多个参数 [英] How to get multiple parameters with same name from a URL in JavaScript

查看:90
本文介绍了如何从JavaScript中的URL获取具有相同名称的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题,但可能没有结束,我如何从URL获取变量。在我的所有搜索中,我发现了几种非常好的方法从网址获得 A = aValue

A question asked without possibly an end is how do I get variable from a URL. In all my searching I have found several very good ways to get A=aValue from the url.

但是我的问题是
我需要

But my question is I need

?Company=FLHS&Device=Crosstown_PCC_01&A=aValue&A=secondAValue

我需要在网址中输入两个A的数组,我需要知道 aValue 是第一个, secondAValue 是第二个

I need an array of the two A's in the url and I need to know that aValue was the first one and secondAValue was the second

我有jquery Mobile。

I have jquery Mobile.

更新

所以这就是我现在所拥有的

So this is what I have now

var urlParamDevice = getURLParameter('DeviceID');


function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
    );

getURLParameter(name)需要更强大一些。

The getURLParameter(name) needs to be a little more robust.

更新2,2014 / 03/07
以下是我从建议的答案中得出的结果

Update 2, 2014/03/07 Here is what I came up with from the suggested answer

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;
}


推荐答案

function getparamNameMultiValues(paramName){
	    var sURL = window.document.URL.toString();
	    var value =[];
	    if (sURL.indexOf("?") > 0){
	        var arrParams = sURL.split("?");
	        var arrURLParams = arrParams[1].split("&");
	        for (var i = 0; i<arrURLParams.length; i++){
	            var sParam =  arrURLParams[i].split("=");
	            console.log(sParam);
	            if(sParam){
		            if(sParam[0] == paramName){
		            	if(sParam.length>0){
		            		value.push(sParam[1].trim());
		            	}
		            }
	            }
	        }
	    }
	    return value.toString();
	}

这篇关于如何从JavaScript中的URL获取具有相同名称的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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