javascript - Passing a variable between HTML pages using JavaScript
问题描述
I begin with having a user input their name into a text field on the first html page and then click a button to move to a second html page.
How do I store the user's inputed name as a variable that can be referenced on a differnt html page?
First HTML page
<input type = "text" id = "name">
<button onclick = "sv(); window.location.href = 'second.html'">Submit now</button>
JavaScript on First HTML page
function sv() {
sessionStorage.setItem("theirname", document.getElementById("name").value);
}
Second HTML page
<p id="output"></p>
JavaScript on Second HTML Page
document.getElementById("output").value = sessionStorage.getItem("theirname");
解决方案
您可以将数据存储到 localStorage
let input = document.getElementById('input');
setStorage = () => {
localStorage.setItem('name', input.value);
}
//On your second HTML
getStorage = () => {
localStorage.getItem('name');
}
<input type="text" id="input">
<button onclick="setStorage()">Go!</button>
推荐阅读
- mysql - 是否可以创建一个带有 hh:mm 时间列的表?
- oracle - 甲骨文偏度
- vba - 我可以在不启用 .NET Framework 3.5 的情况下在 VBA 中使用 ArrayList 吗?
- javascript - 解析来自 expressjs 摩根温斯顿中间件的 json 请求时出错
- php - 创建自定义消息状态码
- php - 具有不同数量值的嵌套 foreach()
- node.js - npm install module -g 给出 MaxListenersExceededWarning
- javascript - 限制滚动和调整大小事件
- javascript - 这是使用 JS 中相同元素的 eventListener 执行单击事件的正确纯代码吗?
- javascript - 我应该如何将滚轮或滚动事件从一个元素转移到另一个元素?