使用localStorage检索页面刷新时先前检查的复选框

时间:2014-08-19 17:51:22

标签: javascript jquery checkbox local-storage

我有一个html代码,其中选中复选框并单击一个按钮,复选框的值将通过函数发送。当用户检查checkbbox时,还会检查相应的chidren复选框。如果我刷新页面,我想检索以前检查过的复选框。我尝试使用localStorage存储复选框值。

localStorage.setItem("check", check);

然后在另一个函数中尝试通过执行

来检索它
    localStorage.getItem("check");
        if(check){
        $("input[name='favorites']").attr("checked", "checked");
$(check)
        .closest('li')
        .find('input:checkbox')
            .prop('checked', $(this).prop('checked') );
}

但这不起作用。我只需要检索先前在页面刷新时检查过的复选框。

我的演示完整代码

HTML

<ul class="test_ex">
    <li>
        <input type="checkbox" id="chckBox" class="parent" /> <a class="ref">Fruits</a>

        <ul class="example">
            <li>
                <input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Apple </a>

            </li>
            <li>
                <input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Orange </a>

            </li>
        </ul>
        <ul class="test_ex_sample">
            <li>
                <input type="checkbox" id="chckBox" class="parent" /> <a class="ref">Birds</a>

                <ul class="example">
                    <li>
                        <input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Peacock </a>

                    </li>
                    <li>
                        <input type="checkbox" id="chckBox" class="child" /> <a class="ref">Parrot </a>

                    </li>
                </ul>
                <ul class="example">
                    <li>
                        <input type="checkbox" id="chckBox" class="parent" /> <a class="ref"> Food </a>

                        <ul class="example">
                            <li>
                                <input type="checkbox" id="chckBox" class="child" /> <a class="ref">Bread </a>

                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
<input type="button" id="testB" value="OK" />

的javascript

$(function() {
    // Clicking on an <input:checkbox> should check any children checkboxes automatically
    $('input:checkbox').change( fnTest );

    // Clicking the OK button should run submit(), pop up displays all checked boxes
    $('#testB').click( submit );
});


function fnTest(check) {
    // Set all child checkboxes to the same value
    $(check)
        .closest('li')
        .find('input:checkbox')
            .prop('checked', $(this).prop('checked') );
}

function submit(check) {
    // When you click OK, dipslay the label for each checkbox
    var checked = [];
    $('input:checkbox:checked').each(function() {
        checked.push( $(this).next('a').text() );
    });
    alert("You have selected:\n\n - " + checked.join("\n - ") );
}

Updated Fiddle

2 个答案:

答案 0 :(得分:1)

在执行类似这样的

时将localStorage分配给变量
var check = localStorage.getItem("check");

编辑:

         $(function() {
// Clicking on an <input:checkbox> should check any children checkboxes automatically
$('input:checkbox').change( fnTest );

// Clicking the OK button should run submit(), pop up displays all checked boxes
$('#testB').click( submit );

display();
    });


    function fnTest(e) {
   // Set all child checkboxes to the same value
    alert($(this).attr("id"));
     localStorage.setItem("prevChecked",$(this).attr("id"));
     localStorage.setItem("checkedState",$(this).prop('checked'));
   $(this)
    .closest('li')
    .find('input:checkbox')
        .prop('checked', $(this).prop('checked') );
 }


function submit(check) {
// When you click OK, dipslay the label for each checkbox
 var checked = [];
$('input:checkbox:checked').each(function() {
    //localStorage.setItem("prevChecked",check);
    checked.push( $(this).next('a').text() );
});
alert("You have selected:\n\n - " + checked.join("\n - ") );
}
    function display(){
     if(localStorage.getItem("checkedState")!="false")
    {
    var test=localStorage.getItem("prevChecked");
   alert(test);
    alert($("#"+test).closest('li').html());
    $("#"+test).closest('li')
    .find('input:checkbox')
        .prop('checked', localStorage.getItem("checkedState") );
        }
    }

链接到您更新的小提琴: http://jsfiddle.net/3a050r6r/36/

答案 1 :(得分:1)

尝试更改此代码并查看它是否适合您:

$(function() {
  // Clicking on an <input:checkbox> should check any children checkboxes automatically
  $('.ref').each(function(){
    var $t = $(this);
    $t.siblings('input').attr('data-name',$t.text());
  });

  $('input:checkbox').change( fnTest );

  // Clicking the OK button should run submit(), pop up displays all checked boxes
  $('#testB').click( submit );

  display();
});

function fnTest(e) {
  // Set all child checkboxes to the same value
  $(this)
    .closest('li')
    .find('input:checkbox')
        .prop('checked', $(this).prop('checked') );
}

function submit(check) {
  // When you click OK, dipslay the label for each checkbox
  var checked = [];
  $('input:checked').each(function() {
     checked.push( $(this).attr('data-name'));
  });
  alert("You have selected:\n\n - " + checked.join("\n - ") );
  localStorage.setItem("prevChecked",checked.join('|'));
}

function display(){
  var test=localStorage.getItem("prevChecked");
  alert(test);
  var results = test.split('|');
  for (var r=0, len=results.length; r<len; r++ ) {
    $('input[data-name="'+results[r]+'"]').prop('checked', true);
  }
}

<强>已更新 看到这个小提琴: http://jsfiddle.net/3a050r6r/6/