javascript - 将值从一个页面传递到另一个页面

标签 javascript jquery html

我想将表单值从一个 html 页面传递到另一个 html 页面。

我想知道是否可以在不使用服务器端代码的情况下在 html 页面之间传递值。

如果没有,我该如何传递值? (我宁愿不使用服务器端...)

这是我的一些项目代码:

第一个 HTML 页面

 <form id="myForm" method="post" action="secondHTMLPage URL">
      <input type="hidden" id="UserID" name="UserID" value="12345">
 </form>
 <script type="text/javascript">
      $("#myForm").submit();
 </script>

第二个 HTML 页面

 <script type="text/javascript">
      // In here, I want to retrieve my $("#myForm")'s data from First Page.
 </script>

最佳答案

你可以试试Window.sessionStorageWindow.localStorage .

The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.

localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.

在一页中设置值

localStorage.setItem('myCat', 'Tom');
//using sessionStorage 
//sessionStorage.setItem('myCat', 'Tom');

获取其他页面的值

var cat = localStorage.getItem('myCat');
//using sessionStorage 
//var cat = sessionStorage.getItem('myCat');

关于javascript - 将值从一个页面传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58759146/

相关文章:

javascript - jQuery Animate 宽度卡住 iPad

html - 如何将文本区域的默认大小与最小大小分开设置?

html - 尝试将无序列表居中

javascript - 在没有 Node.js 和 CDN 的网页中包含 Chart.js

javascript - Cakephp 2.x - 隐藏/显示组合框

javascript - 离线加载本地JSON文件

javascript - 如何将 "this"发送到事件绑定(bind)语句中的命名函数?

javascript - 有没有在 Firefox 浏览器中检测 Windows-11 和 Windows-10 的方法?

javascript - React JS onChange 事件没有被触发

html - 创建我自己的图标字体 .ttf .eot .woff 和 svg 文件