vendredi 20 mars 2015

Using local storage in angular to store which checkboxes were checked

I have a list that can be sorted through checkboxes. I wrote up a little script that should store the check values so on refresh the filtered list shows the same items, as they did before leaving the page.


Currently when I refresh it checks all of my checkboxes, not just the one I checked.


Here is how my inputs are set up:



<input type="checkbox" name="filters" ng-click="includeBrand('Brand A')" />Brand A


and here is my function that should keep the same ones checked:



$(function () {
var data = localStorage.getItem("filter-by");

if (data !== null) {
$("input[name='filters']").attr("checked", "checked");
}


});



$("input[name='filters']").click(function () {

if ($(this).is(":checked")) {
localStorage.setItem("filter-by", $(this).val());
} else {
localStorage.removeItem("filters");
}

});


What could be going wrong as to make it check "all of the above"?


Thanks for the help!





Aucun commentaire:

Enregistrer un commentaire