jeudi 28 avril 2016

Checkbox states change after navigating to another page

I am trying to persist the checkbox states after user traverse to another page on my rails web site.

Currently, when i navigate from index page to another page and come back to the index page checkboxes from index page get checked. I want it to hold the values I give even if I navigate to another page.

I have written

function addingChecks() {
    var filterStatusArr = [];
    filterStatusArr = localStorage.filterStatus;
    $('#filterText h4 .onoffswitch .onoffswitch-checkbox').each(function() {
        for (var i = 0; i < filterStatusArr.length; ++i) {
            if (filterStatusArr[i] == "true") {
                $(this).prop('checked', true);
            } else {
                $(this).prop('checked', false);
            }
        }
    });
};

rememberFilterState();
addingChecks();
function rememberFilterState() {

    filterStatus = [];
    $('#filterText h4 .onoffswitch .onoffswitch-checkbox').each(function() {
        if ($(this).is(':checked')) {
            filterStatus.push("true");
        } else {
            filterStatus.push("false");
        }
        localStorage.input = filterStatus;
    });
    //alert(filterStatus);
}

Works well onload but this ignores when I go to another page and come back.

Is this the approved behavior? How can I fix this?

Cheers




Aucun commentaire:

Enregistrer un commentaire