1

点我

$('.clickme').click(function(event) {
event.preventDefault();
var stringId = $(this).attr("id");
    var mId = stringId.substring(2)
....

我可以使用锚元素的 ID 检索 id 的值。我想我应该可以直接从href中得到它。那么如何从 HREF(url 查询字符串)中检索 id 和 status 的值?

我正在使用 Jquery。

感谢您的帮助。

更新:另外我怎样才能获得所有的 URL 值.. 即“test.php?id=100&blah=blah”?

4

6 回答 6

3

这段代码:

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

要使用它:

document.write(querySt("id"));
document.write(querySt("status"));

回答您的“更新”:

http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml

于 2009-07-23T13:29:36.453 回答
1
var stringId = $(this).attr("id"); // this will return p_100
var stringId = $(this).attr("id").split('_')[1]; // this will return 100

var attr= $(this).attr("href"); // this will return all href attribute value

更新

//href="test.php?id=100&status=pending&time=2009"
var attrFromAnchor= $(this).attr("href").split('?')[1].split('&')[0].split('=')[1]; // returns 100
于 2009-07-23T13:30:02.663 回答
1

这里有很多好的解决方案,但我想我会发布自己的。这是我放在一起的一个快速小函数,它将解析来自 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;
}

要在不解析字符串的情况下从 URL 获取查询字符串的值,您可以执行以下操作:

window.location.search.substr(1)

如果要在 ? 之前的页面名称?你仍然需要做一些字符串解析:

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

希望这可以帮助!干杯。

于 2009-07-31T15:50:42.257 回答
0

这是从查询字符串中获取所有名称/值对的简洁(但完整)实现:

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);
于 2009-09-14T21:38:47.433 回答
0

不需要 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 "";
}
于 2012-10-15T14:04:54.197 回答
0

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

使用Vanilla JavaScript (ES5)查看此解决方案

var qd = {}; // qd stands for query dict
document.getElementById("p_100")[0].href.split("?")[1].split("&").forEach(function(item) {var k = item.split("=")[0], v = decodeURIComponent(item.split("=")[1]); (k in qd) ? qd[k].push(v) : qd[k] = [v,]})

我喜欢假装它是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"

*它返回数组,因为它针对多值键进行了优化。在此处查找虚拟解决方案(没有数组)。

注意:要教旧浏览器新的,你可以从 Mozilla (MDN).forEach注入这个 polyfill 。

于 2015-01-20T13:20:01.863 回答