【发布时间】:2014-03-10 05:00:46
【问题描述】:
我有一个带有选择选项的页面,我正在使用 JQuery 刷新页面并在单击选项时将字符串添加到 url。现在我需要一种方法来检查浏览器 url 以查看它是否包含所述字符串。
查看其他线程,我认为indexOf 会起作用,但是在尝试这个时它不起作用。我还要如何检查 URL 是否包含 ?added-to-cart=555 之类的内容?完整的 URL 通常看起来像:http://my-site.com,在单击其中一个选项后,页面重新加载后看起来像这样:http://my-site.com/?added-to-cart=555。我只需要检查 URL 是否包含 ?added-to-cart=555 位。
这是我所拥有的:
jQuery("#landing-select option").click(function(){
$('#product-form').submit();
window.location.href += $(this).val()
});
jQuery(document).ready(function($) {
if(window.location.indexOf("?added-to-cart=555") >= 0)
{
alert("found it");
}
});
【问题讨论】:
-
您没有注意到控制台中的错误
TypeError: window.location.indexOf is not a function吗? Learn how todebug JavaScript。阅读MDN documentation aboutwindow.location也很有帮助。 -
更新到
window.location.href.indexOf("string-to-match")
标签: javascript jquery url