jQuery:如何获取url的参数?
jQuery 新手,我在获取服务器生成的 url 参数时遇到问题。我有一个像这样的网址:
<span class="popuptest"><a href="www.example.com/test?param1=1¶m2=2">find param</a></span>
我的 jquery 函数看起来像这样:
$(function() {
$('.popuptest a').live('click', function(event) {
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = this.href.slice(this.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
var second = getUrlVars()["param2"];
alert(second);
return false;
});
});
单击链接应该显示“2”,但是我什么也没得到......对 jQuery 菜鸟有什么帮助吗?提前致谢!
我在博客上找到了这个: http: //jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
New to jQuery and I'm having trouble getting the parameters of a url that my server generates. I have a url like this:
<span class="popuptest"><a href="www.example.com/test?param1=1¶m2=2">find param</a></span>
my jquery function looks like so:
$(function() {
$('.popuptest a').live('click', function(event) {
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = this.href.slice(this.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
var second = getUrlVars()["param2"];
alert(second);
return false;
});
});
clicking on the link should show me "2", however I get nothing... any help for a jQuery noob? thanks in advance!
I found this on a blog: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为此,您不需要 jQuery,您可以使用纯 JavaScript:
并且您可以通过这种方式调用该函数..
getParameterByName(param1,href of your link)
演示
You don't need jQuery for that purpose you can use the pure JavaScript:
and you can call the function in this way..
getParameterByName(param1,href of your link)
DEMO
小改进,仅解析一次 url,返回数组或参数:
http://jsfiddle.net/shakhal/gXM3u /
small improvement, parse the url only once, return array or params:
http://jsfiddle.net/shakhal/gXM3u/
很简单,你所要做的就是将 URL 中你想要的变量名称放入这个函数中,然后它就会返回 URL 变量的值
it's so easy, all you have to do is put the variable name that you want it from the URL into this function then it will return you the value of URL variable
Jquery 具有用于获取参数形式 URL 的构建函数。
Jquery have in build functions for get param form URL.
你的jquery可能需要
注意:
Your jquery could be
things to note:
来自此处
from here
如此简单,您可以使用任何 url 并在 Javascript 中获取值。这是完整的代码集
So simple you can use any url and get value in Javascript.And this is Complete set of codes
使用下面的代码有一段时间了,但最近发现 URL 中的 base64_encode 字符串有问题,末尾有 ==,例如: /index.php?a=1&b=2&c=Mw==&d =4。
使用 getUrlVar("c") 给出“Mw”结果,但这应该是“Mw==”,所以添加了一行:
Using code below for some time, but recently discoverd a problem with an base64_encode string in the URL, having == at the end, like: /index.php?a=1&b=2&c=Mw==&d=4.
Using getUrlVar("c") gives me 'Mw' as result, but this should be 'Mw==', so added an extra line: