简体   繁体   中英

Storing check boxes in local storage

So I am having issues figuring out how to store marked check boxes in local storage, and have them refresh on a separate page.

Here is my javascript on the first page with the check boxes.

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

Here is my HTML for that page

<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>

And here is my load function on a separate page

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

I am at a total loss to what I am doing wrong. I know this has been answered before but I am just not "getting it" Any help would be appreciated!

You have not provided any code that would store the check state in localStorage, only code that reads the check state.

In your function storemyform you need to make use of localStorage.setItem(name, value) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM