簡體   English   中英

jQuery更新cookie值

[英]jQuery update cookie value

我正在使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM