jquery从url中获取查询字符串
问题描述:
我试图从url中读取查询字符串,并根据查询字符串提醒消息。字符串正在完美传递,我只是在阅读页面时遇到麻烦。我究竟做错了什么?jquery从url中获取查询字符串
可能的查询字符串
.../playlist.html?vid=1
.../playlist.html?vid=2
JS
$(function() {
if (window.locaion.search.indexOf('vid=1') > -1) {
alert('1');
} else if (window.locaion.search.indexOf('vid=2') > -1) {
alert('2');
}
});
答
两个问题:输入了错误的位置,你需要的toString(),因为位置ISN”一个字符串。所以更正后的版本:
$(function() {
if (window.location.toString().indexOf('vid=1') > -1) {
alert('1');
} else if (window.location.toString().indexOf('vid=2') > -1) {
alert('2');
}
});
+0
...我试图纠正位置错字,它仍然不会为我工作。将.search更改为.toString() - 但也许.search是一个jQuery的东西,我没有加载。 – Rophuine 2012-02-10 22:53:34
答
变化window.locaion
到window.location
locaion是一个错字吗? – 2012-02-10 22:46:15
你已经写了'locaion'而不是'location'。在你的运行代码中是否有错字,或者只是在这里的问题? – Rophuine 2012-02-10 22:46:49
错字?尝试位置,而不是你有什么。 – 2012-02-10 22:46:54