Zapamiętywanie stanów checkbox w cookie

0

Mam kilka checkboxów, chciałbym by po ich ustawieniu były zapamiętane w cookie i po ponownym wpisaniu adresu były zaznaczone tak jak poprzednio. Znalazłem kod http://jsfiddle.net/4ALFU/81/ który działa bardzo fajnie po zaznaczeniu checkboxów, ale nie zapamiętuje ich odznaczania. Co tu nie gra?

1

Bo kodów się nie "znajduje" i nie wkleja do swoich projektów bez myślenia. Ten kod w ogóle nie ma takiego założenia, żeby pamiętać odznaczanie checkboxów.

Poza tym - mamy prawie 2015, nie używałbym do tego cookies, ale niech Ci będzie

//===== Cookies Plugin=====   //

            (function ($) {
                $.cookie = function (key, value, options) {

                    // key and at least value given, set cookie...
                    if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
                        options = $.extend({}, options);

                        if (value === null || value === undefined) {
                            options.expires = -1;
                        }

                        if (typeof options.expires === 'number') {
                            var days = options.expires, t = options.expires = new Date();
                            t.setDate(t.getDate() + days);
                        }

                        value = String(value);

                        return (document.cookie = [
                            encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                            options.path ? '; path=' + options.path : '',
                            options.domain ? '; domain=' + options.domain : '',
                            options.secure ? '; secure' : ''
                        ].join(''));
                    }

                    // key and possibly options given, get cookie...
                    options = value || {};
                    var decode = options.raw ? function (s) { return s; } : decodeURIComponent;

                    var pairs = document.cookie.split('; ');
                    for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
                        if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
                    }
                    return null;
                };
            })(jQuery);

            //======================================//

            JSON.d = function(string) {
                try { return JSON.parse(string); }
                catch (ignored) { return string; }
            };


            $(document).ready(function () {
                var checkbox = $('#boxlawreg').find(':checkbox'), checkboxCookieName = 'checkbox-state';
                var raw_list = $.cookie(checkboxCookieName);
                var list = JSON.d(raw_list);
                if (raw_list!==list) {
                    console.log('parse success', list);
                    var cbs = $('#boxlawreg').find('input[type=checkbox]');
                    console.log(cbs);
                    $.each(list, function(key, val) {
                        cbs.filter(function() { return this.name === val; }).prop('checked', true).change();
                    });
                }
                else {
                    list = [];
                }

                checkbox.click(function () {
                    console.log('click', this.checked, this.name);
                    var index = list.indexOf(this.name);
                    console.log(index);
                    if (this.checked) {
                        if (index === -1) {
                            list.push(this.name);
                            $.cookie(checkboxCookieName, JSON.stringify(list));
                        }
                    }
                    else if (index!==-1) {
                        list.splice(index,1);
                        $.cookie(checkboxCookieName, JSON.stringify(list));
                    }
                    console.log(list);
                });
            });
0
dzek69 napisał(a):

Poza tym - mamy prawie 2015, nie używałbym do tego cookies

A co proponujesz?

1 użytkowników online, w tym zalogowanych: 0, gości: 1