mardi 2 juin 2015

Local storage not saving the checkbox unchecked stage

I am using jquery and local storage to save the state of a checkbox.

For saving I use:

function save_form() {
    window.localStorage.setItem('fb_show', $('#fb_show').val());
    alert('Saved');
}

function load_form() {
    var fb_checkbox_val = window.localStorage.getItem('fb_show');
    alert(fb_checkbox_val);

    if (fb_checkbox_val == 'on') {
        $("#fb_show").prop("checked", true);
    } else {
        $("#fb_show").prop("checked", false);
    }
}

The problem is that when it's unchecked it will save the value as 'on' so that part worked BUT when I uncheck the checkbox and save it again, it still saves as 'on' ... so the code after that will set it as checked again.

How can I fix this?




Aucun commentaire:

Enregistrer un commentaire