我正在使用jquery-cookie库使用JQuery创建cookie。如何更新 Cookie 的值?我需要它创建新的cookie,如果cookie存在以更新它。我该怎么做?我得到的代码:
v.on('click', function(){
var d = $(this).attr('role');
if(d == 'yes')
{
glas = 'koristan.'
}else {
glas = 'nekoristan.'
};
text = 'Ovaj komentar vam je bio ' + glas;
//This part here create cookie
if(id_u == 0){
$.cookie('010', id + '-' + d);
}
$.post('<?php echo base_url() ?>rating/rat_counter', {id : id, vote : d, id_u : id_u}, function(){
c.fadeOut('fast').empty().append('<p>' + text).hide().fadeIn('fast');
});
})
要更新 cookie,您需要做的就是创建一个具有相同名称和不同值的 cookie。
编辑
要将新值附加到旧值...
//Im not familiar with this library but
//I assume this syntax gets the cookie value.
var oldCookieValue = $.cookie('010');
//Create new cookie with same name and concatenate the old and new desired value.
$.cookie('010', oldCookieValue + "-" + id);
注意这个链接
http://www.electrictoolbox.com/jquery-cookies/
在这里,您可以看到您可以使用 cookie 执行的所有重要操作。
如果您想知道 cookie 是否已经存在,只需使用这个
if($.cookie("example") != null)
{
//cookie already exists
}