在网上商店产品页面中,我想用jquery cookie将产品保存为收藏夹。
但是当我点击保存时,我总是收到消息,产品已保存为最爱,如果产品已保存。我从来没有得到过这个产品,这个产品已经在最受欢迎的名单上了。
$('#favorite_button').click(function()
{
var cookie_message, item_id, most_van;//most_van means, that the cookie exists with the procut ID
item_id = <?php echo $kat_id ?>; // product ID
var cookie_val = $.cookie("kedvenc_termek_cookie");
if (cookie_val)
{
most_van = cookie_val.split("|");
}
else
{
most_van = [];
}
if ($.inArray(item_id, most_van) === -1)
{
most_van.push(item_id);
$.removeCookie('kedvenc_termek_cookie');
$.cookie('kedvenc_termek_cookie', most_van.join("|"), { expires: 7, path: "/" });
cookie_message = "Product saved as favorite.";
}
else
{
cookie_message = "This product is alredy on your favorite list.";
}
$('#FavoritItemModalResult').html(cookie_message);
$('#FavoritItemModal').modal('show');
setTimeout(function()
{
$('#FavoritItemModal').modal('hide');
}, 3000 );
});
答案 0 :(得分:0)
您的Cookie Split()
分隔符不正确。它是;
,而不是|
:
var cookies = [];
document.cookie.split(/; */).forEach(function(cookieraw){
var cookie = this.split('=');
var name = cookieraw[0];//print it
var value = cookieraw[1]; //print it
cookies.push({ "name": name, "value": value });
});