简体   繁体   English

单击按钮后如何在不刷新页面的情况下显示本地存储的内容?

[英]How do I display the contents of local Storage after button click without refreshing the page?

I want the contents of the local Storage to be displayed when the submit button is clicked without having to refresh the page. 我希望单击提交按钮时显示本地存储的内容,而不必刷新页面。 I don't see the changes made until I manually refresh the page 在手动刷新页面之前,我看不到所做的更改

This function handles the page load. 此功能处理页面加载。 When a button is clicked it shows wrong data. 单击一个按钮时,它显示错误的数据。 The correct data is only shown when the page is manually refreshed. 仅当手动刷新页面时,才显示正确的数据。

const loadData = () =>
  document.querySelector('body').addEventListener('load', displayStorage());

This is the event listener that handles saving: 这是处理保存的事件监听器:

notesForm.addEventListener('submit', e => {
  e.preventDefault();
  const save = (sid, spost, sdate) => {
    const obj = { id: sid, post: spost, date: sdate };
    localStorage.setItem(`${sid}`, JSON.stringify(obj));
  };
  save(generateId(), post.value, dateFormat());
  loadData();
});

The loadData function is only adding an event listener. loadData函数仅添加事件侦听器。 It is not displaying the data in localStorage. 它不会在localStorage中显示数据。

Try replacing the loadData() call in the submit callback with displayStorage() . 尝试用displayStorage()替换loadData()回调中的loadData()调用。

try providing the function as a handler to be invoked by the load event. 尝试将函数提供为由load事件调用的处理程序。 Not invoke it by yourself 不要自己调用

const loadData = () =>
  document.querySelector('body').addEventListener('load', displayStorage);

You can try reloading the page by yourself using location.reload 您可以尝试使用location.reload自己重新加载页面

notesForm.addEventListener('submit', e => {
  e.preventDefault();
  const save = (sid, spost, sdate) => {
    const obj = { id: sid, post: spost, date: sdate };
    localStorage.setItem(`${sid}`, JSON.stringify(obj));
  };
  save(generateId(), post.value, dateFormat());
  loadData();
  location.reload()
});

暂无
暂无

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

相关问题 更新本地存储中的值并显示在网页上,无需刷新页面 - Update the value in local storage and display it on web page without refreshing the page 单击按钮后如何避免页面刷新 - How to avoid page refreshing after button click 尝试在单击按钮时显示新的随机值而不刷新页面 - Trying to display new random value at the click of a button without refreshing page 如何在不刷新的情况下在同一页面上显示上传的文件内容? - how to display the uploaded file contents on the same page without refreshing? 如何在不同页面上显示来自本地存储的此信息 - How do I display this information from the local storage on a different page 使用本地存储刷新页面后如何保存用户输入 - How to save user inputs after refreshing the page using local storage 如果我在本地存储中存储了令牌,刷新页面后如何保持登录状态 - How to stay logged in after refreshing the page if I have token stored in local storage 仅在按钮导航到另一页后刷新页面后,才能添加到本地存储 - Adding to local storage only working after refreshing the page after button navigation to another page 如何在不刷新jquery AJAX中的页面的情况下返回按钮单击? - How to go back on a button click without refreshing page in jquery AJAX? 为什么刷新页面后我在页面上的输入消失了,但是输入保存在控制台的本地存储中? - Why do my inputs on the page disappear after refreshing the page, but the inputs are saved in local storage in the console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM