读取 url JQuery 中的 GET 变量 [英] read the GET variables in url JQuery

查看:16
本文介绍了读取 url JQuery 中的 GET 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于另一个简单"的问题很抱歉,但是有没有一种简单的方法可以从 URL 读取 GET 变量.例子.我有一个 url http://www.domain.com/page.php?var1=1 在我的情况下,我只有 1 个变量,即 var1var2(该变量可以更改,但每个 url 只有一个).我所看到的所有 tuts 都与数组而不是单例"有关,好吧,我知道数组解决方案可能更好,但这只是一个简单的单个 get 变量.有什么建议?提前致谢

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] 是您的 var 名称,而 split[1] 是您的 var 值.对于那段代码,您实际上并不需要 jQuery ;)

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 ;)

至于 twiz 的评论,可以这样拆分多个变量:

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('=');
});

您可以通过split[index][0]访问变量名,通过split[index][1]访问值.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆