简体   繁体   English

如何添加/更新查询字符串链接到现有URL的末尾

[英]How to add/update query string link to end of exisiting URL

I have existing product filters on my side bar, although I'm trying to have functional links that can update or add to the filters on the products. 尽管我尝试使用功能链接来更新或添加到产品上的过滤器,但我的侧边栏上已有产品过滤器。 snippet of current page 当前页面的摘要

Currently I have the links with < a href="?filter_caster-type=swivel" > although when using that link it removes the current queries. 目前,我具有带有<a href =“?filter_caster-type = swivel”>的链接,尽管使用该链接会删除当前查询。 Is there an easy way to have a link that updates or adds to the current queries? 有没有一种简单的方法可以使链接更新或添加到当前查询?

If you are able to use URLSearchParams : 如果您能够使用URLSearchParams

// assumes the URL already contains a query string, better checking will be needed if this isn't always the case
var paramsFromURL = new URLSearchParams(document.location.search.substring(1));

paramsFromURL.set("WhateverYouNeedToAdd", "TheValueYouWant")

// gives something like http://example.com/path/?ExistingThing=foo&WhateverYouNeedToAdd=TheValueYouWant
var updatedURL = document.location.origin + document.location.pathname + "?" + URLSearchParams.toString();

To add the query into your current URL, you can doing by a javascript function, something like this: 要将查询添加到当前URL中,您可以通过javascript函数执行以下操作:

$(function(){
  $('a').on('click', function(e){
       e.preventDefault();
       window.location.replace(window.location.href + $(this).attr('href'));
  });
}); 

This function take your current URL and append the filter that you have in your a tag and then reload. 此功能把你目前的网址,并将你在你的过滤器a标签,然后重新装入。

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

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