mardi 23 décembre 2014

Store checkbox value in sessionStorage

Can any one help me I'm trying to store the state of checkboxes by using window.sessionStorage. When the user goes from page1 to page2 and then click back button to go to page1 I want all the checkboxes which the user checked before going to page2 needs to be checked.


The below code works only after it is in the sessionStorage. It is not working When the user come to the page1 for the first time and select the checkboxes on page1 and go to page2 and then click on back button it does not working.


//store check box values /*if(typeof(Storage) !== "undefined") {



$('.page-class form input, .page-class form textarea').each(function(){



var getInputName = $(this).attr('name');
var content = sessionStorage.getItem(getInputName);

if($(this).is('input[type="checkbox"]') === true) {
//if this is a checkbox...
var numberCheck = $(this).parent().parent().children().find('input').index(this);
//...determine if this is checkbox 1, 2, 3, etc....
getInputName = getInputName+numberCheck;
//...then assign it a unique identifier based on its name + index
content = sessionStorage.getItem(getInputName);

if(content != null) {
$(this).attr('checked','checked');
//If there is a value in sessionStorage based on this identifier, check the box.
}

$(this).bind('click',function(){
if($(this).attr('checked') == 'checked') {
sessionStorage[getInputName] = $(this).val();
//If checked, store
} else {
sessionStorage.removeItem(getInputName);
//If it becomes unchecked, remove from storage
}
});
} else {
if(content !== null) {
$(this).val(content);
}
$(this).bind('keyup focusout',function(){
sessionStorage[getInputName] = $(this).val();
});
}
});
}*/




Aucun commentaire:

Enregistrer un commentaire