gpt4 book ai didi

javascript - 使用 JQuery 检查 url 是否包含字符串

转载 作者:行者123 更新时间:2023-12-03 21:32:08 25 4
gpt4 key购买 nike

我有一个包含选择选项的页面,我正在使用 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");
}
});

最佳答案

Use Window.location.href to take the url in javascript. it's a property that will tell you the current URL location of the browser. Setting the property to something different will redirect the page.

if (window.location.href.indexOf("?added-to-cart=555") > -1) {
alert("found it");
}

关于javascript - 使用 JQuery 检查 url 是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com