繁体   English   中英

为每个复选框设置jQuery cookie

[英]set jQuery cookie for each checkbox

我有一个简单的jQuery函数,可根据复选框状态切换类。 这是代码:

jQuery的:

  $('input[name$="bg-noise-option"]').click(function(){
    var targetClass = $(this).data('at');
    $('.' + targetClass).toggleClass('bg-noise');
  });

HTML:

  <div class="checkbox">
    <label>
      <input type="checkbox" name="bg-noise-option" data-at="page-wrap" checked>
      Body <br>
    </label>
    <label>
      <input type="checkbox" name="bg-noise-option" data-at="btn">
      Elements <br>
    </label>
    <label>
      <input type="checkbox" name="bg-noise-option" data-at="header, .navbar">
      Top Header <br>
    </label>
    <label>
      <input type="checkbox" name="bg-noise-option" data-at="page-header-wrap">
      Page Header <br>
    </label>
    <label>
      <input type="checkbox" name="bg-noise-option" data-at="site-footer">
      Footer <br>
    </label>
  </div>

问题:如何为每个选中的复选框创建一个cookie? 我只是想给这些复选框一些内存...不确定最好的方法。

我怀疑某种类型的.each(function)应该可以做到,但是我仍然是javascript新手。 我尝试了以下方法:

$.each(function(){
  $.cookie('bg-noise-cookie', $(this).data('at'), {expires:365, path: '/'});
})

但是,当然,这只会为最近选中的复选框创建一个cookie。 如何为每个创建唯一的cookie? 还是我什至需要? 也许有一种方法可以将它们存储在一个cookie中(一个数组?),然后在页面加载时简单地引用该数组以检查复选框?

有很多见识的义务。

假设您正在使用JQuery Cookie插件 ,因为我看到您在问题中引用了它。

这个给你。 单击任何复选框都会创建和更改单个cookie文件,仅存储与复选框对应的data-at值。

在页面加载时,它会检索该JSON字符串,将其转换回一个对象,然后循环遍历,对任何匹配属性的框进行勾选。

经过测试的代码可以正常工作。

HTML:我在您的复选框中添加了[[]],以对它们进行正确分组,并删除了所有默认复选框。

<div class="checkbox">
    <label>
        <input type="checkbox" name="bg-noise-option[]" data-at="page-wrap">
        Body <br>
    </label>
    <label>
        <input type="checkbox" name="bg-noise-option[]" data-at="btn">
        Elements <br>
    </label>
    <label>
        <input type="checkbox" name="bg-noise-option[]" data-at="header, .navbar">
        Top Header <br>
    </label>
    <label>
        <input type="checkbox" name="bg-noise-option[]" data-at="page-header-wrap">
        Page Header <br>
    </label>
    <label>
        <input type="checkbox" name="bg-noise-option[]" data-at="site-footer">
        Footer <br>
    </label>
</div>

jQuery:使用$ .cookie()插件

<script type="text/javascript">
    $(document).ready(function() {
        // check for cookie on load
        if( $.cookie('chk_ar') ) {
            console.log($.cookie('chk_ar'));
            var chk_ar = $.cookie('chk_ar');

            // convert string to object
            chk_ar = $.parseJSON(chk_ar);
            console.log(chk_ar);

            // loop through and apply checks to matching sets
            $.each(chk_ar, function(index, value) {
                console.log(value);
                $('input').filter('[data-at="'+value+'"]').prop('checked', true);
            });
        }
    });

    // handle checkboxes
    $('input[name="bg-noise-option[]"').on('click', function() {
        var check_array = [];

        // add to array
        $(':checked').each(function() {
            check_array.push($(this).attr('data-at'));
        });

        // stringify array object
        check_array = JSON.stringify(check_array);
        console.log(check_array);

        // set cookie
        $.cookie('chk_ar', check_array, {path:'/'});
    });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM