简体   繁体   English

如何检查url是否在JQuery / Javascript中包含字符串

[英]How to check if url contains string in JQuery/Javascript

I have a select box with several options, when a user clicks on an option then I have a string appended to the url and the page will refresh (with string) which will add a product to cart. 我有一个带有多个选项的选择框,当用户单击一个选项时,我在URL后面附加了一个字符串,页面将刷新(带有字符串),这会将产品添加到购物车。

My problem is if the users clicks an option and that string is appended to the url, then when the page is refreshed and they select another option then it just keeps adding strings. 我的问题是,如果用户单击一个选项并将该字符串附加到URL,则在刷新页面并选择另一个选项时,它只会继续添加字符串。

Here is what I mean, I click on one of the options in the select and this appends itself to the url "?added-to-cart=55" , when the page refreshes that string is still in the url, however if I select another option then it just keeps adding to it like this: "http://my-site.com?added-to-cart=55/?added-to-cart=2/?added-to-cart=100" and so on. 这是我的意思,我单击select中的一个选项,然后在页面刷新时将其自身附加到URL "?added-to-cart=55" ,但是如果我选择了该字符串,另一个选择,然后它会像这样继续添加: "http://my-site.com?added-to-cart=55/?added-to-cart=2/?added-to-cart=100"和以此类推。

I am looking for a way to check if there is a string in the url, if so then remove it so when they select a new option then it will just become "http://my-site.com/?added-to-cart=55" 我正在寻找一种方法来检查url中是否有字符串,如果是,则将其删除,这样当他们选择一个新选项时,它将变成"http://my-site.com/?added-to-cart=55"

Here is what I have been working on: I don't know if it would be best to add the below to the click event of my select, or the jQuery(document).ready(function($){}); 这是我一直在努力的事情:我不知道是否最好将以下内容添加到我选择的jQuery(document).ready(function($){});的click事件中jQuery(document).ready(function($){}); .

<script type="text/javascript">
jQuery("#landing-select option").click(function(){
    $('#product-form').submit();
    window.location.href += $(this).val();



});

jQuery(document).ready(function($) {
    // This is what I have to look for the string and to remove it? 
    var url = window.location.href;
    url = url.split('?')[0];
    window.history.pushState(“string”, “Title”, “newUrl”);
});

</script>

So the values of my options contain the "?added-to-cart=…" part. 因此,我的选项的值包含"?added-to-cart=…"部分。 Basically when a user clicks an option I need it to add the options value to the url which it does perfectly, then the page refreshes with the product added and string still in url. 基本上,当用户单击一个选项时,我需要它来将选项值添加到完美完成的url中,然后页面刷新,其中添加了产品,并且字符串仍在url中。 I just need a way to remove any existing strings similar to "?added-to-cart=" when a new option (product) is clicked, so it will add the new string to the url and refresh to add product to cart rather than just keep adding strings to the url. 我只需要一种方法,即可在单击新选项(产品)时删除类似于"?added-to-cart=" add "?added-to-cart="所有现有字符串,因此它将新字符串添加到url并刷新以将产品添加到购物车,而不是只需将字符串添加到url。

I really hope I made sense here, seems basic but I may have confused the question. 我真的希望我在这里说得通,似乎很基本,但是我可能已经把这个问题弄糊涂了。

Thanks 谢谢

You only need to overwrite location.search , not the entire url: 您只需要覆盖location.search ,而不是整个URL:

jQuery("#landing-select option").click(function(){
   $('#product-form').submit();
   window.location.search = $(this).val();
});

this will just load the given url with a new querystring, regardless what the old one was. 不管旧的是什么,这只会使用新的查询字符串加载给定的url。

Get full path then check position of '?' 获取完整路径,然后检查“?”的位置

1) window.location.href or document.URL 1)window.location.href或document.URL

2) indexOf() 2)indexOf()

Override the query string only. 仅覆盖查询字符串。 Something like: 就像是:


jQuery("#landing-select").change(function(){ //change is more appropriate than click
   jQuery('#product-form').submit();
   window.location.search = jQuery(this).val();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM