Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用javascript(Jquery)检索查询字符串参数和值?_Php_Javascript_Jquery_Query String_Anchor - Fatal编程技术网

Php 如何使用javascript(Jquery)检索查询字符串参数和值?

Php 如何使用javascript(Jquery)检索查询字符串参数和值?,php,javascript,jquery,query-string,anchor,Php,Javascript,Jquery,Query String,Anchor,点击我 $('.clickme').click(function(event) { event.preventDefault(); var stringId = $(this).attr("id"); var mId = stringId.substring(2) .... 我可以使用锚元素的id检索id的值。我想我应该可以直接从href获取它。那个么,如何从HREF(url查询字符串)检索id和状态的值呢 我正在使用Jquery 谢谢你的帮助 更新: 还有,我如何获得所有URL值。。

点击我

$('.clickme').click(function(event) {
event.preventDefault();
var stringId = $(this).attr("id");
    var mId = stringId.substring(2)
....
我可以使用锚元素的id检索id的值。我想我应该可以直接从href获取它。那个么,如何从HREF(url查询字符串)检索id和状态的值呢

我正在使用Jquery

谢谢你的帮助

更新: 还有,我如何获得所有URL值。。i、 e.“test.php?id=100&blah=blah”?

此代码:

function querySt(ji) {
    hu = $(".clickme").attr("href");
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
回答您的“更新”:

此代码:

function querySt(ji) {
    hu = $(".clickme").attr("href");
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
回答您的“更新”:

更新

//href="test.php?id=100&status=pending&time=2009"
var attrFromAnchor= $(this).attr("href").split('?')[1].split('&')[0].split('=')[1]; // returns 100
更新

//href="test.php?id=100&status=pending&time=2009"
var attrFromAnchor= $(this).attr("href").split('?')[1].split('&')[0].split('=')[1]; // returns 100

这里有很多好的解决方案,但我想我会发布自己的。 下面是我创建的一个快速的小函数,它将以window.location.search或提供的搜索字符串值的格式解析查询字符串

它返回id值对的散列,以便您可以以下列形式引用它:

var values = getQueryParams();
values['id']
values['blah']
代码如下:

/*
 This function assumes that the query string provided will
 contain a ? character before the query string itself.
 It will not work if the ? is not present.

 In addition, sites which don't use ? to delimit the start of the query string
 (ie. Google) won't work properly with this script.
 */
function getQueryParams( val ) {
    //Use the window.location.search if we don't have a val.
    var query = val || window.location.search;
    query = query.split('?')[1]
    var pairs = query.split('&');
    var retval = {};
    var check = [];
    for( var i = 0; i < pairs.length; i++ ) {
        check = pairs[i].split('=');
        retval[decodeURIComponent(check[0])] = decodeURIComponent(check[1]);
    }

    return retval;
}
如果要在页面名称之前输入页面名称?您仍然需要执行一些字符串分析:

var path = window.location.pathname.replace(/^.*\/(.*)$/,'$1');
var query = path + window.location.search;
//If your URL is http://www.myserver.com/some/long/path/big_long%20file.php?some=file&equals=me
//you will get: big_long%20file.php?some=file&equals=me
希望这有帮助!
干杯。

这里有很多好的解决方案,但我想我会发布自己的。 下面是我创建的一个快速的小函数,它将以window.location.search或提供的搜索字符串值的格式解析查询字符串

它返回id值对的散列,以便您可以以下列形式引用它:

var values = getQueryParams();
values['id']
values['blah']
代码如下:

/*
 This function assumes that the query string provided will
 contain a ? character before the query string itself.
 It will not work if the ? is not present.

 In addition, sites which don't use ? to delimit the start of the query string
 (ie. Google) won't work properly with this script.
 */
function getQueryParams( val ) {
    //Use the window.location.search if we don't have a val.
    var query = val || window.location.search;
    query = query.split('?')[1]
    var pairs = query.split('&');
    var retval = {};
    var check = [];
    for( var i = 0; i < pairs.length; i++ ) {
        check = pairs[i].split('=');
        retval[decodeURIComponent(check[0])] = decodeURIComponent(check[1]);
    }

    return retval;
}
如果要在页面名称之前输入页面名称?您仍然需要执行一些字符串分析:

var path = window.location.pathname.replace(/^.*\/(.*)$/,'$1');
var query = path + window.location.search;
//If your URL is http://www.myserver.com/some/long/path/big_long%20file.php?some=file&equals=me
//you will get: big_long%20file.php?some=file&equals=me
希望这有帮助! 干杯。

下面是一个简洁(但完整)的实现,用于从查询字符串中获取所有名称/值对:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {};
    var tokens;

    while (tokens = /[?&]?([^=]+)=([^&]*)/g.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

//var query = getQueryParams(document.location.search);
//alert(query.foo);
下面是一个简洁(但完整)的实现,用于从查询字符串中获取所有名称/值对:

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {};
    var tokens;

    while (tokens = /[?&]?([^=]+)=([^&]*)/g.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

//var query = getQueryParams(document.location.search);
//alert(query.foo);

不需要jQuery,此解决方案适用于所有浏览器:

function querySt(ji)
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
    ft = gy[i].split("=");
    if (ft[0] == ji) {
    return ft[1];
    }
    }
    return "";
}
函数查询(ji)
{
hu=window.location.search.substring(1);
gy=hu.分割(“&”);

对于(i=0;i无需jQuery),此解决方案适用于所有浏览器:

function querySt(ji)
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
    ft = gy[i].split("=");
    if (ft[0] == ji) {
    return ft[1];
    }
    }
    return "";
}
函数查询(ji)
{
hu=window.location.search.substring(1);
gy=hu.分割(“&”);

对于(i=0;i而言,这里的答案现在已经过时了

使用香草JavaScript(ES5)查看此解决方案 我喜欢假装它是oneliner,但有人告诉我不是。嗯……谁会在新行上拆分链式函数调用,对吗

例如:

"test.php?id=100&status=pending&time=2009"
> qd
id: ["100"]
status: ["pending"]
time: ["2009"]

// values can also be obtained like this
> qd.id[0]    // "100"
> qd["id"][0] // "100"
*它返回数组,因为它是针对多值键优化的


注意:要教旧浏览器新的
。forEach
您可以从Mozilla(MDN)注入。

这里的答案现在已经过时了

使用香草JavaScript(ES5)查看此解决方案 我喜欢假装它是oneliner,但有人告诉我不是。嗯……谁会在新行上拆分链式函数调用,对吗

例如:

"test.php?id=100&status=pending&time=2009"
> qd
id: ["100"]
status: ["pending"]
time: ["2009"]

// values can also be obtained like this
> qd.id[0]    // "100"
> qd["id"][0] // "100"
*它返回数组,因为它是针对多值键优化的



注意:要教旧浏览器新的
。forEach
您可以从Mozilla(MDN)注入。

感谢您的回答。.我可以从元素ID(已完成)获取值。.但我需要从锚的URL获取值。感谢您的回答。.我可以从元素ID(已完成)获取值…但我需要从锚的URL获取它。好的..所以我必须通过for循环找到它?我想我可以直接访问它..类似于attr(“status”)!!!现在你可以,只需执行querySt(“status”);我认为jQuery没有用于URI解析的内置函数。这将适用于除id之外的所有内容。使用此代码的关键是“test.php?id”而不是“id”。您需要首先删除“?”之前的所有内容然后在符号上分开。在那里加入一些decodeURIComponent调用怎么样?好的。所以我必须通过for循环找到它?我想我可以直接访问它。类似于attr(“status”)!!!现在你可以,只需执行querySt(“status”)即可;我认为jQuery没有用于URI解析的内置函数。这将适用于除id之外的所有内容。使用此代码的关键是“test.php?id”而不是“id”。您需要首先删除“?”之前的所有内容然后在符号上拆分。在其中加入一些decodeURIComponent调用如何?此实现不考虑URL编码的名称和值。@您完全正确。它不考虑这种情况。我已根据您的建议更新了解决方案。如果没有查询字符串,还应添加一个条件在初始化之后发送,即
if(!query)返回[];
query.split('?')[1]之前
line此实现不考虑URL编码的名称和值。@您完全正确。它不考虑这种情况。我已根据您的建议更新了解决方案。如果初始化后没有查询字符串,也应添加一个条件,即。
if(!query)return[]
query.split(“?”)[1]
line单行regexp不适用于safari和IE(无限循环)。解决方法是为“var pattern=/[?&]?([^=]+)=([^&]*)/g;”单独设置一行,然后使用“pattern.exec(qs)”。顺便问一下,“qs.split(“+”)是什么对于?如果url是编码的,则不应该有加号。单行regexp不适用于safari和IE(无限循环)。修复方法是为“var pattern=/[?&]?([^=]+)=([^&]*)/g;”,使用“pattern.exec(qs)”。顺便问一句,“qs.split”(“+”)是什么例如?如果url已编码,则不应有加号。您需要对其进行子串。如果需要示例,请留下注释。您需要对其进行子串。如果需要示例,请留下注释。