简体   繁体   English

如何获取一页中输入字段的值并在另一页中打印?

[英]How to get the value of input field in one page and print it in another page?

How can I get the value of an input field and print it on another HTML page?如何获取输入字段的值并将其打印在另一个 HTML 页面上? Here is my first page:这是我的第一页:

index.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>

I want to print that "name" variable on the "page.html".我想在“page.html”上打印那个“name”变量。 Please help.请帮忙。

I would suggest you to use localStorage, set this value:我建议你使用 localStorage,设置这个值:

localStorage.setItem('nameOfVariable', value);

and after loading other page you can get the value with加载其他页面后,您可以使用

localStorage.getItem('nameOfVariable')

In your case:在你的情况下:

function setName() {
   var name = document.getElementById('firstName').value;
   localStorage.setItem('name', name);
}

in page.html:在页面中。html:

function getName() {
       var name = localStorage.getItem('name');
       // Do something
    }

Try to use localStorage .尝试使用localStorage

var name = document.getElementById('firstName').value;
window.localStorage.setItem('name', name);

And then in page.html page,然后在page.html页面,

Try to get name尝试获取名称

const name = window.localStorage.getItem('name')

And then print it.然后打印出来。

You need to use the localStorage in the variable name like您需要在变量名中使用localStorage

document.getElementById("firstName").value = localStorage.name

You can acces the localStorage data from an another page like您可以从另一个页面访问 localStorage 数据,例如

document.getElementById("name").innerHTML = localStorage.name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM