I have a problem with this code. This is a product page, and if i click the save button, it saves the product ID into a jquery cookie. In the cookie, i store the ID-s like this example: 1,2,3,4....
If an ID is already in the cookie, i dont get that error, it always saves it again, than the ID will be duplicated in the cookie.
$('#favorite_button').click(function()
{
var cookie_message, item_id, most_van;
item_id = <?php echo $kat_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 = "ID saved okey.";
}
else {
cookie_message = "This ID already in the cookie.";
}
$('#FavoritItemModalResult').html(cookie_message);$('#FavoritItemModal').modal('show');
setTimeout(function()
{
$('#FavoritItemModal').modal('hide');
}, 3000 );
});
$.inArray is like Array.prototype.indexOf, it strictly compares (===).
When you do most_van = cookie_val.split(",");, you get an array of strings. Make sure you have the same types.
If item_id
is an int, then you sould do:
most_van = cookie_val.split(",");
most_van = most_van.map(function(id) {
return parseInt(id, 10)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.