Remember checkbox value
Hi I would be so grateful if anyone could shed some light on this, Basically I have a iphone app built using jqtouch and phonegap which has a list of standard checkboxes and when they are se开发者_如何学JAVAlected a strikethrough effect will be applied, easy enough (kind of like a simple checklist) this is all dealt with on the client side and nothing will be getting sent anywhere.
However, when the user closes the app the checkboxes revert to their original sate i.e all unchecked and i would like them remain as the user left them. I guess local storage would be the way to do this, however, i have gave it a go and i am getting a bit confused.
Any help would be great, and thank you in advance.
kyle
Here you go:
$(document).ready(function() {
$('form')
.delegate(':checkbox','change',function() {
var $this = $(this);
localStorage.setItem('checkbox' + $this.index('form :checkbox'),$this.attr('checked').toString());
})
.find(':checkbox')
.each(function(index) {
if(localStorage.getItem('checkbox' + index) === 'true') $(this).attr('checked',true);
});
});
Took some time to write it correctly myself but I think it's worth it. The default state handling is up to you (like when a checkbox is already checked with HTML).
精彩评论