读取URL JQuery中的GET变量 [英] read the GET variables in url JQuery
问题描述
对不起,有另一个简单问题,但有没有简单的方法来从URL中读取GET变量。例。我有一个URL http://www.domain.com/page.php?var1=1
在我的情况下,我将只有1个变量,即 var1
或 var2
(变量可以改变,但每个url只会有一个)。我所见过的所有内容都与数组相关,而不是单例。我知道一个数组解决方案可能会更好,但这只是一个简单的单个获取变量。有什么建议么?在此先感谢
var split = location.search.replace('?' '').split('=')
split [0]
是你的var名称,并且 split [1]
是你的var值。你真的不需要用jQuery代替那段代码;)
至于twiz的评论,分裂多个变量可以这样做:
var split = location.search.replace('?','').split('&')。map(function(val){
return val.split('=');
});
您可以通过 split [index] [0] code>和$
。
当然,您可以使用第二个片段,而不是第一个为一个变量。
Sorry for another "simple" question, but is there an easy way to read the GET variables from a URL. example. I have a url http://www.domain.com/page.php?var1=1
In my case I will only have 1 variable i.e. var1
or var2
(the variable can change but there will only every be one per url). All the tuts I have seen relate to arrays rather than "singletons" OK I know an array solution may be better but this is just a simple single get variable. Any suggestions? Thanks in advance
var split = location.search.replace('?', '').split('=')
split[0]
is your var name, and split[1]
is your var value. You actually don't really need jQuery for that piece of code ;)
As for twiz's comment, splitting multiple variables can be done like that:
var split = location.search.replace('?', '').split('&').map(function(val){
return val.split('=');
});
You can access variable name by split[index][0]
and value by split[index][1]
.
Of course you can use the second snippet instead of the first one for one variable too.
这篇关于读取URL JQuery中的GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!