如何使用按钮重定向到查询链接



我正在处理一个具有"example.com/data/?name=xx&location=yy"查询url的项目。我用get方法做了这个,使项目更容易。但我遇到了一个小问题。我无法重定向到带有表单数据的url。

我尝试过用获取表单的输入值

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);
};

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

window.locationhref=goToURL;是怎么回事?

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

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

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

最新更新