简体   繁体   中英

jQuery update cookie value

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:

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'); 
      });
    })

To update a cookie all you need to do is create a cookie with the same name and a different value.

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);

watch out for this link

http://www.electrictoolbox.com/jquery-cookies/

here you see all important thing you can do with cookies.

if you want to know if an cookie already exists, just use this

if($.cookie("example") != null)
{
    //cookie already exists
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM