首页 > 解决方案 > 如何使用javascript使输入值显示在不同的页面中?

问题描述

我正在尝试使我的一个 html 页面中的输入值显示在我的另一个 html 页面中?

我的代码在第一页如下。

  <div class="resume-input">
    <h3>SUMMARY: </h3>
    <textarea name="Summary" placeholder="If its more than 100 words no one is going to read it" id="textarea" cols="30" rows="10"></textarea>
  </div>

这是我在页面中应该显示输入值的 html 代码

  <div class="col-md-12">
    <center>
      <h1><div id="summary"></div></h1>
    </center>
  </div>

我的 JS 看起来像这样

document.querySelector('#button1').addEventListener('click', function(){
  var summary = document.querySelector('#textarea').value;
  var summaryInput = document.querySelector('#summary');
  summaryInput.textContent = summary
  window.location = 'results.html'
})

window.location = 'results.html' 允许按钮将输入值重定向到具有上一页输入值的另一个页面。

标签: javascripthtmlformsinputproject

解决方案


在按钮上单击将值存储在localStorage

localStorage.setItem("anyKeyName","here goes the value from textarea")

并在加载results.html检索值

summaryInput.textContent = localStorage.getItem("anyKeyName")

也看看window.onload


推荐阅读