简体   繁体   English

删除传递给URL作为数组的URL查询字符串之一?

[英]Remove one of URL query strings which passed to URL as array?

Here is the URL : 这是网址:

www.example.com/?param%5B%5D=A&param%5B%5D=B

the %5B%5D part is [] to pass param as an array, which is encoded in url. %5B%5D部分为[]以将param作为数组传递,并以url编码。

Now, I want to remove one of parameters , desired output is: 现在,我要删除parameter之一,所需的输出是:

www.example.com/?param%5B%5D=B

I have searched for this but found nothing! 我已经搜索过,但是什么也没找到! All the answers are about removing a single value parameter, not multiple. 所有答案都是关于删除单个值参数,而不是多个参数。

UPDATE: 更新:

I don't know the exact position of the parameter, ie the URL could be something like this: 我不知道参数的确切位置,即URL可能是这样的:

www.example.com/?test=124&test2=456&param%5B%5D=A&param%5B%5D=B

Here is an example on how to extract the params from the URL. 这是有关如何从URL提取参数的示例。

Now how to use them on a user user interaction form (UI) is up to you. 现在由您决定如何在用户用户交互表单(UI)上使用它们。

 // Use this to get the document location: var ActualURL = document.location.href; console.log("This snippet URL: "+ActualURL); // Only for this demo, I "simulate" a URL. // ActualURL is overwritten here. var ActualURL = "www.example.com/?param%5B%5D=A&param%5B%5D=B"; console.log("FAKED URL: "+ActualURL); var domain = ActualURL.split("?")[0]; console.log("Domain: "+domain); var params = ActualURL.split("?")[1]; var param_array = params.split("&"); for (i=0;i<param_array.length;i++){ console.log( "Param #"+i+": "+param_array[i] ); } console.log("Rebuilted URL with only param #2: "+domain+"?"+param_array[1]); 

You can take advantage of URL WebAPI. 您可以利用URL WebAPI。

https://developer.mozilla.org/en-US/docs/Web/API/URL https://developer.mozilla.org/zh-CN/docs/Web/API/URL

 var base = 'http://www.example.com/' var query = '?param%5B%5D=A&param%5B%5D=B'; var url = new URL(base + query); var params = new URLSearchParams(url.search); var filteredParams = params.getAll('param[]') .filter(function(el) { return el !== "A"; }).map(function(el){ return ['param[]', el]; }); var newParams = new URLSearchParams(filteredParams); var url = new URL(base + '?' + newParams.toString() ); console.log(url.toString()); 

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

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