简体   繁体   English

如何从URL中获取多个逗号分隔值

[英]How do I get multiple comma separated values from URL

I have a URL like: 我有一个像这样的网址:

http://www.mysite.com/index.html?x=x1&x=x2&x=x3

How do I got the values like below, using JavaScript or JQuery: 我如何使用JavaScript或JQuery获得如下的值:

var x='x1,x2,x3'
var url = "http://www.mysite.com/index.html?x=x1&x=x2&x=x3";
var params = url.match(/\?(.*)$/)[1].split('&');
var values = [];
for(var i=0; i<params.length; i++){
    values.push( params[i].match(/=(.*)$/)[1] );
}
var result = values.join(","); // "x1,x2,x3"

EDIT : Here is a better solution that lets you select the parameter you want. 编辑 :这是一个更好的解决方案,可让您选择所需的参数。 This is something that I have found buried inside one of my projects, and I didn't write every part of it. 这是我发现埋在我的一个项目中的东西,我没有写出它的每一部分。

function $_GET(param) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    var values = [];
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (urldecode(pair[0]) == param) {
            values.push(urldecode(pair[1]));
        }
    }
    return values.join(",");
}

// Decode URL with the '+' character as a space
function urldecode(url) {
  return decodeURIComponent(url.replace(/\+/g, ' '));
}

If you directly hit url you can use it as 如果您直接点击网址,则可以将其用作

var fieldValue = ['x1','x2','x3'];
var searchValue = 'x='+ fieldValue.join(',');
window.location.search = searchValue;

This will hit current url to search data for given parameters. 这将命中当前url以搜索给定参数的数据。

If you want to manually create url then hit search then 如果您想手动创建网址,则点击搜索

var url = "http://www.mysite.com/index.html"; 
window.location.href = url;
var fieldValue = ['x1','x2','x3'];
var searchValue = 'x='+ fieldValue.join(',');
window.location.search = searchValue;

Now you can search values, as per requirement. 现在,您可以根据需要搜索值。

I think what you need is PURL. 我认为你需要的是PURL。 Please refer https://github.com/allmarkedup/purl for detailed usage and guidelines 有关详细用法和指南,请参阅https://github.com/allmarkedup/purl

function GetUrlValue(VarSearch){
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for(var i = 0; i < VariableArray.length; i++){
    var KeyValuePair = VariableArray[i].split('=');
    if(KeyValuePair[0] == VarSearch){
        return KeyValuePair[1];
    }
}

} }

read here http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html 在这里阅读http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html

You can easily find query string in jquery using jquery split 您可以使用jquery split在jquery中轻松查找query string

Try this function to get Query String as a array object: 尝试使用此函数将Query String作为数组对象获取:

function getUrlVars()
{
    var vars = [];
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[1]);             
    }
    return vars;
}

The function returns an array/object with your URL parameters and their values. 该函数返回一个数组/对象,其中包含您的URL参数及其值。 So, you can use jquery .join() to convert it into comma separated values: 因此,您可以使用jquery .join()将其转换为逗号分隔值:

var result = vars.join(",");

Try in jsfiddle 试试jsfiddle

Maybe use Regex: 也许使用正则表达式:

var s = window.location.search;
var foo = s.match(/x=([0-9a-zA-Z]+)/g).join(",").replace(/x=/g, ""); // x1,x2,x3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM