刷新页面但保持复选框已选中

时间:2015-10-10 01:34:34

标签: javascript jquery checkbox

我知道我是一个直接的菜鸟,我已经尝试了很多不同的方法来让这个jsfiddle像我想要的那样工作。但仍然没有运气。

我链接到第一个复选框保留其

的版本

http://jsfiddle.net/Acree/wv2us4od/14/

<input type="checkbox" id="yolo">
<input type="checkbox" id="yolo2">
<input type="checkbox" id="yolo3">


$(function(){
var test = localStorage.input === 'true'? true: false;
$('#yolo').prop('checked', test || false);
});

$('#yolo').on('change', function() {
localStorage.input = $(this).is(':checked');
console.log($(this).is(':checked'));
});

基本上我需要所有复选框彼此独立保存。 上下文是这样的,用户可以检查他们可以做的事情,当他们回到网站时它会在那里。 http://pk-unity.com/tricks/index2.html

1 个答案:

答案 0 :(得分:2)

Demo

if(localStorage.length > 0)
{
    $.each(localStorage,function(index,item)
    {
        if( $("#"+index)[0] )
        { 
            if(item == "true")
                $("#"+index).attr("checked","checked");
        }
    });
}
$('input:checkbox').on('change', function() 
{
    localStorage.setItem($(this).attr("id"),($(this).is(":checked") ? true : false));
});
相关问题