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

ここでの回答は古くなっています。

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を挿入できます。

于 2015-01-20T13:20:01.863 に答える
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 に答える