Javascript jQuery 更新 cookie 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11902737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery update cookie value
提问by Sasha
I am using jquery-cookie library to create cookie with JQuery. How can I update value of the cookie? I need it create new cookie and if the cookie exists to update it. How can I do this? Code that I got:
我正在使用 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');
});
})
回答by marteljn
To update a cookie all you need to do is create a cookie with the same name and a different value.
要更新 cookie,您需要做的就是创建一个具有相同名称和不同值的 cookie。
Edit
编辑
To append your new value to the old...
要将新值附加到旧值...
//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);
回答by Safari
watch out for this link
注意这个链接
http://www.electrictoolbox.com/jquery-cookies/
http://www.electrictoolbox.com/jquery-cookies/
here you see all important thing you can do with cookies.
在这里你可以看到你可以用 cookie 做的所有重要的事情。
if you want to know if an cookie already exists, just use this
如果你想知道一个 cookie 是否已经存在,只需使用这个
if($.cookie("example") != null)
{
//cookie already exists
}