Javascript jQuery 查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3788125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery querystring
提问by oshirowanen
Possible Duplicate:
get querystring with jQuery
可能的重复:
使用 jQuery 获取查询字符串
How do I get the value of a querystring into a textbox using jQuery?
如何使用 jQuery 将查询字符串的值放入文本框?
Lets say the url is http://intranet/page1.php?q=hello
假设 url 是http://intranet/page1.php?q=hello
I would like the "hello" to be in the textbox.
我希望“你好”出现在文本框中。
回答by Guffa
In my programming archiveI have this function:
function querystring(key) {
var re=new RegExp('(?:\?|&)'+key+'=(.*?)(?=&|$)','gi');
var r=[], m;
while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
return r;
}
You can use that to get the query string value and put in a textbox:
您可以使用它来获取查询字符串值并放入文本框中:
$('#SomeTextbox').val(querystring('q'));
回答by Aistina
Use the function listed in the answerto this question:
function getParameterByName( name )
{
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
And then just do something like this:
然后做这样的事情:
var qParam = getParameterByName('q');
$('#mytextbox').val(qParam);