【问题标题】:How to make the input values show up in a different page with javascript? [duplicate]如何使用javascript使输入值显示在不同的页面中? [复制]
【发布时间】:2018-08-27 17:31:12
【问题描述】:

我正在尝试让我的一个 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' 允许按钮将输入值重定向到具有上一页输入值的另一个页面。

【问题讨论】:

  • 要么'results.html?sum='+summary阅读location.search要么尝试本地/会话存储
  • 您可以将值存储在localStorage/sessionStorage 中,然后以这种方式将它们填充到其他页面上。

标签: javascript html forms input project


【解决方案1】:

点击按钮将值存储在localStorage 中使用

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

并在加载 results.html 时检索

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

也看window.onload

【讨论】:

  • 有一个错误:localStorage.setlocalStorage.setItemlocalStorage.getItem 而不是 get
  • @E.Zacarias 为错误道歉,感谢您指出
  • localStorage.set("anyKeyName") 拉“这里是文本区域中的值”然后当你转到 results.html 时你把 localStorage.get("anyKeyName")从上一页的 textarea 中提取值?
  • 没有window.load这样的东西
猜你喜欢
  • 2021-06-20
  • 2019-09-28
  • 2019-01-19
  • 1970-01-01
  • 2023-03-30
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
相关资源
最近更新 更多