(jQuery) Save checkbox state on click in cookie

前端 未结 4 953
春和景丽
春和景丽 2021-02-04 19:26

There are a lot of topics regarding this function, nonetheless I can\'t seem to get it working. I\'ve googled on this specific case and a bunch of links let me here, but strangl

相关标签:
4条回答
  • 2021-02-04 19:45

    You can change ALL your code to just: EDITED to remove part unneeded.

    $(document).ready( function(){
       // read the current/previous setting
        $("input.box[type=checkbox]").each(function() {
            var name = $(this).attr('name');
            if ($.cookie(name) && $.cookie(name) == "true") {
                $(this).prop('checked', $.cookie(name));
            }
        });
       // event management
        $("input.box[type=checkbox]").change(function() {
            var name = $(this).attr("name");
            $.cookie(name, $(this).prop('checked'), {
                path: '/',
                expires: 365
            });
        });
    });
    

    including getting rid of all these:

    $(document).ready( function(){
        remember("[name=1]");
    });
    ...
    

    EDIT: less verbose version:

    $("input.box").each(function() {
        var mycookie = $.cookie($(this).attr('name'));
        if (mycookie && mycookie == "true") {
            $(this).prop('checked', mycookie);
        }
    });
    $("input.box").change(function() {
        $.cookie($(this).attr("name"), $(this).prop('checked'), {
            path: '/',
            expires: 365
        });
    });
    

    working example: http://jsfiddle.net/R73vy/

    0 讨论(0)
  • 2021-02-04 19:48

    It should work with jQuery < 1.6 but starting with 1.6 you should change every appearance of .attr("checked") to .prop("checked")

    EDIT: Changed the if condition for checking the cookie

            function remember( selector ){
                $(selector).each(
                    function(){
                        var name = $(this).attr('name');
                        if( $.cookie( name ) && $.cookie(name)=="true" ){
                            $(this).prop('checked', true);
                        } else {
                          $(this).prop('checked', false)
                        }
                        $(this).change(
                            function(){
                                $.cookie(name, $(this).prop('checked'), { path: '/', expires: 365 });
                            }
                        );
                    }
                );
            }
    
    0 讨论(0)
  • 2021-02-04 19:51

    If you don't strictly need to use cookies, using the newer HTML5 Web Storage (specifically the sessionStorage object in this case) is a lot easier and better than storing in a cookie:

    http://www.w3schools.com/html/html5_webstorage.asp

    Browser support is pretty full:

    http://caniuse.com/#search=sessionstorage

    0 讨论(0)
  • 2021-02-04 19:52

    If you're just interested in one checkbox AND/OR you don't want to include the jQuery cookie plugin for this, here's two lines of code:

    //on page load set the checkbox to stored value or default to true
    $('#turbo').prop('checked' , ( typeof sessionStorage.turbo !== 'undefined' ) ? (sessionStorage.turbo=='true') : true ); 
    //when checkbox is updated, update stored value
    $('#turbo').change( function() { sessionStorage.turbo = $(this).prop('checked');  }); 
    

    This also doesn't use cookies so saves a couple bytes of bandwidth. Change to localStorage to extend lifetime of saved selection.

    0 讨论(0)
提交回复
热议问题