2009-07-23 14 views
1

點擊我如何使用javascript(jquery)檢索查詢字符串參數和值?

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

我可以檢索使用錨元素的ID id的值。我想我應該可以直接從href中獲取它。那麼如何從HREF(url查詢字符串)中檢索id和狀態的值?

我正在使用Jquery。

謝謝你的幫助。

更新: 我該如何獲得所有的URL值?即「test.php?id = 100 & blah = blah」?

+0

你需要對它進行子串處理。如果您需要示例,請發表評論。 – 2009-07-23 13:53:42

回答

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

+0

謝謝你的回答.. – TigerTiger 2009-07-23 13:36:45

+0

好吧..所以我必須通過for循環找到它?我想我可以直接訪問它..像attr(「狀態」)! – TigerTiger 2009-07-23 13:39:30

+0

現在你可以,只是做querySt(「status」);我不認爲jQuery已經獲得了URI解析函數的構建。 – 2009-07-23 13:59:30

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 

UPDATE

//href="test.php?id=100&status=pending&time=2009" 
var attrFromAnchor= $(this).attr("href").split('?')[1].split('&')[0].split('=')[1]; // returns 100 
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 

希望這有助於! 乾杯。

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); 
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 ""; 
} 
0

這裏的答案是現在已過時

請參閱使用香草的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" 

*它返回陣列,因爲它是爲多值密鑰優化。看看here虛擬解決方案(沒有陣列)。

說明:要教老瀏覽器新的.forEach,你可以從Mozilla(MDN)注入this polyfill

相關問題