我的localStorage没有保存任何东西

问题描述 投票:-3回答:1

所以这就像一个ToDo列表网站,但是当我刷新什么都没有保存! 这是HTML和Javascript:

localStorage.setItem('myInput'); {
  var save = localStorage.getItem("myInput");
}
localStorage.setItem('myInput2'); {
  var save = localStorage.getItem("myInput2");
}
localStorage.setItem('myInput3'); {
  var save = localStorage.getItem("myInput3");
}
<div class="modal-content">
  <span class="close">&times;</span>
  <div id="myDIV" class="header">
    <h2>Add a task</h2>
    <input type="text" id="myInput" placeholder="Write a task...">
    <input type="text" id="myInput2" placeholder="Estimated time...">
    <input type="text" id="myInput3" placeholder="Week...">
    <span onclick="newElement()" class="addBtn" value="save" onClick="save()">Add</span>
  </div>
</div>
</div>


<ul id="tehtavat">
</ul>

我也在控制台中收到此错误:

index.html Uncaught TypeError:无法在'Storage'上执行'setItem':需要2个参数,但只有1个存在。

javascript jquery html local-storage
1个回答
2
投票

如果要将值保存到localstorage,则需要将值作为第二个属性传递。第一个是关键。

localStorage.setItem('myInput', myValue);

将localstorage视为key / value对的列表。

此外,您希望每次数据更改时手动将数据写入localstorage,所以让我们说keyup事件:

document.getElementById('myInput').addEventListener('keyup', function(event){
    localStorage.setItem('inputValue', event.target.value);
})

我可以想象,在加载时,你想要带回这个价值;

var inputValue = localStorage.getItem('inputValue');

if(inputValue) {  //just check if there's value saved
    document.getElementById('myInput').value = inputValue;
}

请考虑一下我在没有测试的情况下输入我的代码,因此可能存在一些拼写错误。您也可以尝试自己解决拼写错误。

© www.soinside.com 2019 - 2024. All rights reserved.