JQuery:无需重新加载即可更改 URL 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16217326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:42:39  来源:igfitidea点击:

JQuery: Change URL param without reloading?

jqueryurlurl-rewriting

提问by Jan Meier

I have a question, is it possible to change the URL via JQuery under the following conditions:

我有一个问题,是否可以在以下条件下通过 JQuery 更改 URL:

  • Same URL will load on browser back and on reloads
  • Page doesn't reload when you change the parameter
  • 相同的 URL 将在浏览器返回和重新加载时加载
  • 更改参数时页面不会重新加载

采纳答案by James Carter

If you wanna prevent the page from reloading you'll have to use hash "#"

如果您想阻止页面重新加载,则必须使用哈希“#”

And if you wanna change the URL try that:

如果您想更改 URL,请尝试:

jQuery.param.querystring(window.location.href, 'valueA=321&valueB=123');

This will return the following URL:

这将返回以下 URL:

http://bla.com/test.html?valueA=321&valueB=123

http://bla.com/test.html?valueA=321&valueB=123

回答by Ruben.Canton

I may be wrong but I think the browser will do a post if you change the location.

我可能是错的,但我认为如果您更改位置,浏览器会发帖。

So, could it help you to use a post instead than a GET and change a hiddenfield/just set the value?

那么,它可以帮助您使用帖子而不是 GET 并更改隐藏字段/仅设置值吗?

You can use jQuery.ajax() to make calls to the server setting the values you need in the post (or get, as you wish). That way the url doesn't change and you make the requests you need.

您可以使用 jQuery.ajax() 调用服务器设置您在帖子中需要的值(或获取,如您所愿)。这样 url 就不会改变,你就可以提出你需要的请求。