2013-09-26 34 views
2

我必須從href鏈接中拆分值。使用jquery從href鏈接中獲取值

我的HREF鏈接如下

var val =location.href  // outputs http://localhost:8080/[email protected]&joid=68 

我想單獨獲得username價值和joid價值。

我試着以下但不工作

var emailrogh= val.split("&"); 

email=emailrogh[0]; 

var idrough=emailrogh[1].split("="); 

var id=idrough[1]; 

我怎麼能提取[email protected]68

+3

http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values –

回答

2

您可以使用此方法

var my_link = location.search.substring().split("&"); 
var email = my_link[0].split("="); 
email = email[1]; 
var id = my_link[1].split("="); 
id = id[1]; 
alert(email); 
alert(id); 

我希望這會工作。

0

嘗試

VAR ID = VAL .match(/用戶名=(。*?)(? =(& | $))/)[1]

1

對於「用戶名」參數:

url.split("username=")[1].split("&")[0]返回「[email protected]」。

同爲 「joid」:

url.split("joid=")[1]返回 「68」。

編輯:請參閱this answer以獲取更可靠的方法。

1

您首先需要從href屬性中獲取URl。

var wholeString = $("#MyAncId").attr('href'); 

然後用下面的方法來獲取查詢字符串值

function getParameterByName(wholeString, name) { 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
     results = regex.exec(wholeString); 
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

Source: DextOr

1

您可以使用下面:

var url = "http://localhost:8080/[email protected]&joid=68"; 
var vars = {}, key; 
var querystring = url.slice(url.indexOf('?') + 1).split('&'); 
for(var i = 0; i < querystring.length; i++){ 
     key = querystring[i].split('='); 
     vars[key[0]] = key[1]; 
} 
console.log(vars); 

輸出將是:

Object {username: "[email protected]", joid: "68"} 

以下是演示:http://jsfiddle.net/uEqZs/

1
function GetURLParameter(sParam) 
{ 
    var sPageURL = window.location.search.substring(1); 
    var sURLVariables = sPageURL.split('&'); 
    for (var i = 0; i < sURLVariables.length; i++) 
    { 
     var sParameterName = sURLVariables[i].split('='); 
     if (sParameterName[0] == sParam) 
     { 
      return sParameterName[1]; 
     } 
    } 
}​ 

這是你可以使用這個功能假設URL是,

"http://samplepage.com/?technology=jquery&blog=jquerybyexample". 
1

試試這個小&潔功能:

function getValue(name) { 
    var filter = new RegExp(name + "=([^&]+)"); 
    return unescape(window.location.match(filter)[1]); 
} 

var username = getValue("username"); 
var joid = getValue("joid"); 

請參閱JsFiddle

1

使用此功能

function getParameterByName(name) 
{ 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
     results = regex.exec(location.search); 
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

此功能將直接返回你的價值。

例如。爲您的鏈接

[email protected]&id=68 

使用該功能

var email = getParameterByName("names"); 
var id = getParameterByName("id"); 

值將

email = "[email protected]"; 
id = "68"; 
1

我想這可能幫助

var loc_name = "http://localhost:8080/[email protected]&joid=68&test=test"; 
var get_params = (loc_name.split("?")[1]).split("&"); 
var contents = []; 
for(var i= 0; i < get_params.length ; i++){ 
    contents[(get_params[i].split("=")[0])] = get_params[i].split("=")[1]; 
} 
console.log(contents['username']); 
console.log(contents['joid']); 
console.log(contents['test']); 

這將解析查詢字符串中的數組。我在控制檯中得到以下輸出

[email protected] 
68 
test