如何通过按钮重定向到查询链接?

问题描述 投票:0回答:2

我正在研究一个具有“ example.com/data/?name=xx&location=yy”查询网址的项目。我使用get方法使项目更加容易。但是我陷入了一个小问题。我无法使用表单数据重定向到该网址。

我尝试使用var nameInput = document.getElementById("productNameInput").value;获取表单的输入值,然后将这些值组合到一个url中。然后,我对这些URL进行了重定向调用,但没有成功。

document.getElementById("buttonID").onclick = function () {
  var nameInput = document.getElementById("productNameInput").value;
  var locationInput = document.getElementById("productLocaInput").value;
  var goToURL = "http://example.me/data/add/?" + "name=" + nameInput + "&" + "location=" + locationInput;

   window.location.replace(goToURL);
    };

但是没有错误消息或其他信息。当我按下该按钮时,页面突然刷新,什么也没做。

javascript html node.js dom
2个回答
0
投票

window.location.href = goToURL;怎么了?


0
投票

如果您不想刷新页面,可以尝试此操作

const queryString = `?name=${nameInput}&location=${locationInput}`
history.pushState(null, null, queryString);


请参见[history API]:https://developer.mozilla.org/en-US/docs/Web/API/History_API

© www.soinside.com 2019 - 2024. All rights reserved.