8

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

2 に答える 2

12

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);
于 2012-08-10T13:33:35.620 に答える
1

このリンクに気をつけてください

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

ここでは、Cookieを使用して実行できるすべての重要なことがわかります。

Cookieがすでに存在するかどうかを知りたい場合は、これを使用してください

if($.cookie("example") != null)
{
    //cookie already exists
}
于 2012-08-10T13:36:12.357 に答える