(jQuery) Save checkbox state on click in cookie

前端 未结 4 1012
春和景丽
春和景丽 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: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 });
                            }
                        );
                    }
                );
            }
    

提交回复
热议问题