javascript - javascript打开带有变量的新html
问题描述
我想通过按下此按钮转到“stats.html”,并且我想在第一个站点的“stats.html”代码上写入 var 名称:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function getcube(){
var number=document.getElementById("field").value;
}
function window() {
var number=document.getElementById("field").value;
}
</script>
<form action="stats.html">
<input id="field" type="text" name="name" placeholder="Player Name...">
<input id="button" type="submit" value="SEARCH" onclick="window()">
</form>
</body>
</html>
我不知道我应该在“stats.html”中包含什么。
感谢帮助。
解决方案
一种方法是使用查询参数。
// index.html
<script type="text/javascript">
function redirect() {
const name = document.getElementById("name").value;
location.href = `stats.html?name=${name}`;
}
</script>
<input id="name" type="text" name="name" placeholder="Player Name...">
<input id="button" value="Go!" onclick="redirect()">
// stats.html
<script type="text/javascript">
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get('name');
document.getElementById("name").value = name;
</script>
<input id="name" type="text" name="name" placeholder="Player Name...">
更多信息: 如何在 JavaScript 中获取查询字符串值
推荐阅读
- javascript - 阻止 localStorage 在开发者工具中显示?
- gpu - LSF 中的 ngpus_excl_p
- miniconda - Conda 安装 UnsatisfiableError
- ruby - RubyGem 此 gem 的可用/已安装版本
- vim - 在命令模式下更改删除默认行为
- javascript - 仅反弹 Div 的背景图像而不是内容
- javascript - 使用运行 nodejs 的 javascript 在事件之前发送通知
- javascript - 使用按钮从文本字段中复制信息?
- python - 如何使用 Selenium 和 Python 绕过 Tor 网络设置确认弹出窗口
- mysql - 如果一天内有超过 1 个数据集,如何保留最新的数据集