无法让cookie工作

时间:2012-11-08 05:14:29

标签: jquery cookies

$(document).ready(function(){

   var lastsettings = $.cookie('location');

    if(lastsettings == "blue"){
    $('fieldset').css('background-color', 'blue');
    $('fieldset').css('margin-top', '0px');
        $('fieldset').css('margin-left', '0px');
   }
    else if(lastsettings == "red"){
        $('fieldset').css('background-color', 'red');
        $('fieldset').css('margin-top', '0px');
        $('fieldset').css('margin-right', '0px');
    }
    else if(lastsettings == "yellow"){
        $('fieldset').css('background-color', 'yellow');
        $('fieldset').css('margin-top', '240px');
        $('fieldset').css('margin-left', '0px');
    }
    else if(lastsettings == "green"){
        $('fieldset').css('background-color', 'green');
        $('fieldset').css('margin-top', '240px');
        $('fieldset').css('margin-right', '0px');
    }

$('select').change(function(){
    $('select option:selected').each(function(){
        position = this.text;

        $('fieldset').css('margin', 'auto');

        if(position == "Top Left"){
            $('fieldset').css('background-color', 'blue');
            $('fieldset').css('margin-top', '0px');
            $('fieldset').css('margin-left', '0px');
            $.cookie('position', 'blue');
        }
        else if(position == "Top Right"){
            $('fieldset').css('background-color', 'red');
            $('fieldset').css('margin-top', '0px');
            $('fieldset').css('margin-right', '0px');
            $.cookie('position', 'red');
        }
        else if(position == "Bottom Left"){
            $('fieldset').css('background-color', 'yellow');
            $('fieldset').css('margin-top', '240px');
            $('fieldset').css('margin-left', '0px');
            $.cookie('position', 'yellow');
        }
        else if(position == "Bottom Right"){
            $('fieldset').css('background-color', 'green');
            $('fieldset').css('margin-top', '240px');
            $('fieldset').css('margin-right', '0px');
            $.cookie('position', 'green');
        }
        else{
            $('fieldset').css('background-color', 'white');
            $('fieldset').css('margin', 'auto');
            $('fieldset').css('margin-top', '100px');
        }

    });

});

        $("#invisibility").toggle(function(){
        $('fieldset').css('visibility', 'hidden');
    }, function(){
        $('fieldset').css('visibility', 'visible');
    });

});

我正在使用jquery cookie插件,但是当我刷新页面时,它会恢复正常。我正在编写一个网站,我使用字段集将框移动到某个位置,但是当我刷新页面时,框会移回到原始位置,而不是我在刷新之前将其设置为的位置。

1 个答案:

答案 0 :(得分:0)

好吧,如果你像

那样创建cookie location
 var lastsettings = $.cookie('location');

...然后,下一步是为像

这样的cookie分配一个值
$.cookie('location', 'blue');

...但您要将值设置为不存在的Cookie

$.cookie('position', 'blue');