在带有标记的url上添加GET参数


Adding GET parameter on url with a tag

我有一个url,看起来像这个

mysite.com/index.php?page=home&gender=female&age=22

mysite.com/index.php?page=home&gender=female&occupation=programmer

如何使用a标记添加新的parram orderby=valueorder=asc,就像提交按钮的工作方式一样。

就像当我点击链接名称时,它会在链接上添加新的参数a我可以按它对数据进行排序

<a href="#">Name</a> | <a href="#">Age</a>

比如wordpress如何处理

wordpress/wp-admin/users.php?orderby=login&order=desc

为什么不在模板渲染期间构建URL?无需JS

例如

<a href="/index.php?occupation=programmer&orderby=name&order=desc">Name</a>

只需在渲染和反转时检查当前顺序值。

显然,如果您使用的是AJAX,这就没那么有用了。

您可以使用:

document.URL + "orderby=desc"

唯一的问题是,当您再次单击时,会得到两次orderby。因此,您应该检查url是否已经包含orderby参数。

这里可以找到一个更复杂的函数来设置或替换url参数:

添加/更改URL的参数并重定向到新的URL

使用它

var url = document.URL;
if(url.indexOf('orderby') == -1){
url = document.URL + "orderby=desc";
}

您可以使用此处发布的函数https://stackoverflow.com/a/6021027/1021726.所有荣誉都归于业余

函数可以在查询字符串上创建一个新值,也可以更新现有值。

<a>标记上添加onclick事件,并调用类似的函数

updateQueryStringParameter(window.url, "orderby", "myName");
updateQueryStringParameter(window.url, "order", "desc");