2009-10-26 75 views
5

首先,爲了顯示cookie,我使用了來自electrictoolbox.com的代碼。如何使用這個jQuery插件刪除一個cookie?

然後我做了一個表格添加一些餅乾:

<form class="cokies" method="post"> 
<input class="query" name="q" type="text" /> 
<input type="submit" name="save" value="saving"> 
<a>Delete Cookies</a> 
</form> 
$(document).ready(function(){ 
$('.cokies a').click(function(){ 
    $.cookie('q', null); 
}); 

remember('[name=q]'); 

此功能是komodomedia.com

function remember(selector){ 
    $(selector).each(function(){ 
     //if this item has been cookied, restore it 
     var name = $(this).attr('name'); 
     if($.cookie(name)){ 
      $(this).val($.cookie(name)); 
     } 

     //assign a change function to the item to cookie it 
     $(this).change(function(){ 
      $.cookie(name, $(this).val(), { path: '/', expires: 365 }); 
     }); 
    }); 
} 

的問題是,我不知道如何刪除一塊餅乾。

回答

14

要刪除cookie,只需將expires:設置爲負整數值即可。

例如:

$.cookie(name, $(this).val(), { path: '/', expires: -5 });

+1

是的,這是工作。謝謝 – 2009-10-27 04:52:02

+2

如果它有效,請給它一個答案,其他誰來這個問題知道正確的答案。 – mauris 2009-10-27 06:38:05

2

jQuery的cookie的腳本有一個錯誤....這可能是更好的改變劇本jquery.cookie.js的開頭:

jQuery.cookie = function(name, value, options) { 
if (typeof value != 'undefined') { // name and value given, set cookie 
    options = options || {}; 
    if (value === null) { 
     value = ''; 
     options.expires = -1; 
     options.path = "/"; 

    } 
.... 

在這種情況下,您將能夠按預期刪除cookie。

7

這個Cookie插件的新版本已經問世,並提供以下方便的語法:

$.removeCookie('q'); 
+1

請務必將路徑設置爲與您最初設置Cookie相同的路徑:'$ .removeCookie('q',{path:'/'});' – LessQuesar 2013-08-10 00:56:18

1

$.removeCookie("COOKIE_NAME",{domain:'.domain.com',path:'/'});

檢查cookie的路徑和領域,並確保包括那些多餘的參數與$.cookie插件。