我正在尝试使用 _GET 方法动态获取查询参数。我的功能看起来像这样:var baseURL = "http://example.org";// clears the last query paramshistory.pushState("", document.title, baseURL);// retrieves the value from the HTML codevar value = document.getElementById('id').value;// URL with updated query paramsvar updatedURL = document.URL + "?value=" + value;// pushing the new URL without refreshing the page.history.pushState("", document.title, updatedURL);// URL looks something like this "http://example.org?value=1"但是当我尝试使用_GET['value']从 URL 检索值时,它只获取页面初始化的值并且不会动态更新,有没有什么方法可以在不刷新页面的情况下检索这个值?
1 回答

森林海
TA贡献2011条经验 获得超2个赞
history.pushState()只需更改浏览器 URL 的值。它实际上并不发出服务器请求并加载 _GET 变量。这就是您无法访问它们的原因。
要在不刷新页面的情况下执行此操作,您需要进行 ajax 调用。
$.ajax({
type: "GET",
url: "yourpage.php",
data: {value:value},
cache: false,
success: function(result){
// do something with the returned data
}
});
- 1 回答
- 0 关注
- 130 浏览
添加回答
举报
0/150
提交
取消