在本地存储中存储复选框

时间:2016-03-09 19:50:03

标签: javascript jquery html

所以我在解决如何在本地存储中存储标记的复选框时遇到问题,并在单独的页面上刷新它们。

这是我的第一页上的javascript和复选框。

function storemyform(){ 
var checked = JSON.parse(localStorage.getItem('checkbox1_0'));
if (checked == true)
{
document.getElementById("checkbox1_0").checked = true;
}

以下是该页面的HTML

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
      <legend>Option</legend>
      <input type="checkbox" name="checkbox1" id="checkbox1_0" class="custom" value="" />
      <label for="checkbox1_0">Option</label>
      <input type="checkbox" name="checkbox1" id="checkbox1_1" class="custom" value="" />
      <label for="checkbox1_1">Option</label>
      <input type="checkbox" name="checkbox1" id="checkbox1_2" class="custom" value="" />
      <label for="checkbox1_2">Option</label>
      <input type="checkbox" name="checkbox1" id="checkbox1_3" class="custom" value="" />
      <label for="checkbox1_3">Option</label>
      <input type="checkbox" name="checkbox1" id="checkbox1_4" class="custom" value="" />
      <label for="checkbox1_4">Option</label>
    </fieldset>
  </div>

这是我在单独页面上的加载功能

function getmynote(){
var checked = JSON.parse(localStorage.getItem('checkbox1_0'));
document.getElementById("checkbox1_0").checked = checked;
}

我完全不知道我做错了什么。我知道这已经得到了回答,但我只是没有“得到它”任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您尚未提供任何将检查状态存储在localStorage中的代码,只提供了读取检查状态的代码。

在您的函数storemyform中,您需要使用localStorage.setItem(name, value)