我有一个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能够..
我有一个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能够..
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
var str =“[email protected]&id=68”; var str也包含名称。它不起作用 – Psl
请参阅更新谢谢@Psl –
可能是:
var str = "[email protected]&id=68";
var splitted = str.replace("id=", "").split("&");
console.log(splitted);
//gives ["[email protected]", "68"]
使用此功能
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";
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, " "));
}
选中此[文章] [1]。它有不同的做法。 [1]:http://stackoverflow.com/questions/1403888/get-url-parameter-with-javascript-or-jquery –
使用'Regex',仅此而已。 –