[英]How to get the value of input field in one page and print it in another page?
如何获取输入字段的值并将其打印在另一个 HTML 页面上? 这是我的第一页:
索引.html
<form action="page.html" method="get">
<label>Enter your name:</label><br>
<input type="text" id="firstName" name="firstName"><br>
<input type="submit" name="submit" value="Submit">
</form>
<script>
function myFunction() {
var name = document.getElementById('firstName').value;
</script>
我想在“page.html”上打印那个“name”变量。 请帮忙。
我建议你使用 localStorage,设置这个值:
localStorage.setItem('nameOfVariable', value);
加载其他页面后,您可以使用
localStorage.getItem('nameOfVariable')
在你的情况下:
function setName() {
var name = document.getElementById('firstName').value;
localStorage.setItem('name', name);
}
在页面中。html:
function getName() {
var name = localStorage.getItem('name');
// Do something
}
尝试使用localStorage
。
var name = document.getElementById('firstName').value;
window.localStorage.setItem('name', name);
然后在page.html页面,
尝试获取名称
const name = window.localStorage.getItem('name')
然后打印出来。
您需要在变量名中使用localStorage
document.getElementById("firstName").value = localStorage.name
您可以从另一个页面访问 localStorage 数据,例如
document.getElementById("name").innerHTML = localStorage.name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.