jQuery cookie设置后未定义就行了吗? [英] Jquery cookie undefined on the line after setting it?

查看:176
本文介绍了jQuery cookie设置后未定义就行了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,我只是熟悉jquery cookie,但似乎无法找出设置cookie后未定义cookie的原因.

Good morning all, I'm just getting familiar with jquery cookie, but I can't seem to find out why the cookie is undefined after setting it.

基本上,我正在尝试在cookie中的jQuery ui中使用sortable存储排序顺序,但是在设置cookie之后,它仍然说未定义.我将在下面显示jquery,如果你们需要看更多(html和其他内容,我也可以进行更新以包括在内).

Basically, I'm trying to store the sort order using sortable in jquery ui in a cookie, but after setting the cookie it's still saying undefined. I'll show the jquery below, and if you guys need to see more (the html and what not, I can update to include that too).

首先要注意的是,在可排序的更新功能中,如果我在设置cookie之前放了alert(煮熟的),它将按需要发出item1,item2,item3,item4,item5的警报,因此它不是某种东西它试图设置一个具有null值的cookie(无论是否会引起问题).设置cookie后,我会警告刚刚设置的同一个cookie,并且变得不确定.如果有人对发生的事情有任何想法,那太好了!

First, just to note, in the update function for sortable, if I put alert(cooked) before I set the cookie,it alerts with item1,item2,item3,item4,item5 as it should, so it's not like something where it's trying to set a cookie with a null value (whether that would cause a problem or not). After setting the cookie, I alert the same cookie I just set and am getting undefined. If anyone has any ideas as to what would be going on, that would be great!

$(document).ready(function() {
    restoreOrder();

    var parentHeight = $(".item").parent(".row").height();
    $(".item").height(parentHeight);

    $(".productsContainer").sortable({
        items: '> .row > .item',
        update: function(event, ui) {
            var cooked = [];
            $(".productsContainer").each(function(index, domEle) { cooked[index] = $(domEle).sortable('toArray'); });
            $.cookie('products', cooked.join('|'));
            alert($.cookie('products'));
        }
    });
    $(".productsContainer").disableSelection();

    $(".item").click(function() {
        $(this).children(".item-box").children("p").toggle();
    });

    $(".close").click(function() {
        var parent = $(this).parent();
        var parentId = parent.attr("id");

        var $old = $(this).siblings(".item-box").children("img");
        var $newImg = $old.clone();
        $newImg.addClass("deletedImage")
        $(".deleteContainer").append($newImg);

        var newOffset = $newImg.offset();
        var oldOffset = $old.offset();

        var $temp = $newImg.clone();
        $("body").append($temp);
        $temp.css('position', 'absolute').css('left', oldOffset.left).css('top', oldOffset.top).css('zIndex', 1000);
        $newImg.hide();
        parent.hide();

        $temp.animate( {'top': newOffset.top, 'left':newOffset.left}, 'slow', function(){
            $newImg.show();
            $old.remove();
            $temp.remove();
        });
    });
})

function restoreOrder() {
    var cookie = $.cookie('products');
    alert(cookie);
    if (!cookie) return;
    var SavedID = cookie.split('|');
    for ( var u=0, ul=SavedID.length; u < ul; u++ ){ alert(SavedID[u]); SavedID[u] = SavedID[u].split(',');}
    for (var Scolumn=0, n = SavedID.length; Scolumn < n; Scolumn++) {
        for (var Sitem=0, m = SavedID[Scolumn].length; Sitem < m; Sitem++) {
            $("#sortable").eq(Scolumn).append($("#sortable").children("#" + SavedID[Scolumn][Sitem]));
        }
    }
}

再次非常感谢您的帮助!

Thanks again so much for any help!

推荐答案

如果其他任何人也遇到同样的问题,则是谷歌浏览器存在问题.我在firefox和IE中尝试过,并且cookie可以正常工作.

If anyone else is having this same problem, it's a problem with google chrome. I tried this in firefox and IE and the cookies worked fine.

Chrome似乎禁用了本地文件(例如file:///)上的cookie.我看到的几个解决方案如下:

It appears that chrome disables cookies on local files like file:///. A couple solutions I saw were the following:

1)使用您的本地IP 127.0.0.1或本地主机(未尝试这样做).

1) use your local IP 127.0.0.1 or localhost (didn't try this).

2)使用命令行参数--enable-file-cookies(

2) use the command line argument --enable-file-cookies (

这篇关于jQuery cookie设置后未定义就行了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆