【问题标题】:Retrieve the previously checked checkboxes on page refresh using localStorage使用 localStorage 在页面刷新时检索先前选中的复选框
【发布时间】:2014-10-12 22:24:49
【问题描述】:

我有一个 html 代码,其中复选框被选中,单击按钮时复选框的值通过函数发送。当用户选中复选框时,也会选中相应的子复选框。如果我刷新页面,我想检索以前选中的复选框。我尝试使用 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

【问题讨论】:

  • 仅供参考:ID 在页面上应该是唯一的。 id="chckBox" 只能使用一次!尝试更改为类,或者,如果您需要单独获取它们,为每个元素创建唯一的 ID。
  • 但我没有使用任何类或 id 将它们存储在 localStorage 我只是发送检查的值。
  • 我没有说你是......因此仅供参考。如果您不使用该 ID,请将其删除!
  • 你能用localStorage解决方案展示代码吗?可能就像var check=localStorage.getItem("check"); 一样简单
  • @Gary 先生:这是演示 localStorage 的小提琴。 jsfiddle.net/3a050r6r/2 运行它检查一些复选框单击确定并再次运行它。它给出对象对象警报但不选中框

标签: javascript jquery checkbox local-storage


【解决方案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/

【讨论】:

  • 相同的结果没有任何变化:(
  • 看看上面的小提琴链接。在小提琴中更新了你的代码
【解决方案2】:

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

$(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/

【讨论】:

  • 先生:这个链接和我的小提琴先生一样
  • jsfiddle.net/3a050r6r/6... 这是您的代码。但是我不能为每个字段提供数据名称,因为名称是动态生成的:(
  • 您没有在问题的任何地方指定这一点。我只能通过问题中的内容。由什么动态生成?
  • 我不知道html代码会有变化。对不起。它只是基于一些 db 树生成的树。
  • 好吧,你遇到的问题是你试图在 localStorage 中存储一个 jquery 对象。那是行不通的。您需要一个可以存储的字符串值。您可以尝试使用文本值,但它需要更复杂的 jQuery 才能达到相同的效果。
猜你喜欢
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 2013-08-17
  • 2021-05-17
  • 1970-01-01
相关资源
最近更新 更多