早上好,我只是熟悉jquery cookie,但我似乎无法在设置之后找出为什么cookie未定义。
基本上,我试图在cookie中使用jquery ui中的sortable存储排序顺序,但是在设置cookie之后它还在说未定义。我将在下面显示jquery,如果你们需要看到更多(html和什么不是,我可以更新以包含它)。
首先,请注意,在可排序的更新功能中,如果我在设置cookie之前设置了alert(cooked),它会以item1,item2,item3,item4,item5进行警告,因此它会“#”;不喜欢它试图设置一个空值的cookie(这是否会导致问题)。设置完cookie后,我会提醒我刚设置的同一个cookie并且未定义。如果有人对将要发生的事情有任何想法,那就太棒了!
$(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]));
}
}
}
再次感谢您的帮助!
答案 0 :(得分:0)
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.
It appears that chrome disables cookies on local files like file:///. A couple solutions I saw were the following:
1) use your local IP 127.0.0.1 or localhost (didn't try this).
2) use the command line argument --enable-file-cookies (