2013-09-26 45 views
0

我有一个href链接如下。我必须分别溢出name值和id值。jquery代码拆分href链接

[email protected]&id=68 

使用jQuery如何可以拆分[email protected]68 我尝试以下一个

var val =location.href.split('?')[1] ,其输出所述[email protected]&id=68

我必须输出[email protected] and 68 separately..how能够..

+1

选中此[文章] [1]。它有不同的做法。 [1]:http://stackoverflow.com/questions/1403888/get-url-parameter-with-javascript-or-jquery –

+0

使用'Regex',仅此而已。 –

回答

1
var str="[email protected]&id=68"; 

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

email=emailrogh[0];// show [email protected] 

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

var id=idrough[1];//show 68 

更新

var str="[email protected]&id=68"; 

str=str.split("name=")[1]; 

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

email=emailrogh[0];// show [email protected] 

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

var id=idrough[1];//show 68 
+0

var str =“[email protected]&id=68”; var str也包含名称。它不起作用 – Psl

+0

请参阅更新谢谢@Psl –

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