问题
Possible Duplicate:
Change URL parameters with jQuery?
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.
<div onclick="javascript:window.location.href = 'page.php?action=list-volunteer-apps&view-all=No';">Hide Closed Applications</div>
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.
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
.
This is the way I am doing the append:
onclick="javascript:window.location.assign(window.location.href+='&view-all=No');"
回答1:
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");
});
});
来源:https://stackoverflow.com/questions/13025880/js-append-variable-to-url-without-repeating