JS:追加变量URL不重复 [英] JS: Append variable to URL without repeating
问题描述
可能重复:结果
更改URL参数与jQuery?
目前,我有一个< DIV>
标签,看起来像一个按钮,并与重定向到同一个页面一个onClick函数改变URL只是增加了&安培; view_all =是
或&放大器; view_all =否
来的网址
Currently, I have a <div>
tag that looks like a button and changes the URL with an onClick function which redirect to the same page just adds &view_all=Yes
or &view_all=No
to the URL.
&LT; DIV的onclick =JavaScript的:window.location.href ='?page.php行动=列表的志愿者应用和放大器;查看,所有=否';&GT;隐藏关闭的应用程序&LT; / DIV&GT;
好吧,我增加了新功能的网站,我需要能够追加了&安培; view_all =是
或&安培; view_all =否
来的URL重定向,而不是他们到底是因为我有,我不能失去URL其他变量。
Well, I am adding a new feature to the website and I need to be able to append that &view_all=Yes
or &view_all=No
to the end of the URL instead of redirecting them because I'll have other variables in the URL that I can't lose.
我已经找到了一种方法做追加但它一直添加&放大器; view_all
变量URL的末尾,以便它看起来像这样<$c$c>page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No$c$c>.
I have figured out a way to do the append but it keeps adding the &view_all
variables to the end of URL so it looks like this page.php?action=list-volunteer-apps&view-all=No&view_all=Yes&view_all=No
.
这是我做的追加方式:
<$c$c>onclick=\"javascript:window.location.assign(window.location.href+='&view-all=No');\"$c$c>
推荐答案
您可以使用普通的前pression在替换字符串值:
You can use regular expression to replace the value in a string:
$(this).attr("onclick", function(i, val) {
return val.replace(/view-all=(Yes|No)/, function() {
return "view-all=" + ((arguments[1] || "") == "Yes" ? "No" : "Yes");
});
});
这篇关于JS:追加变量URL不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!