2011-02-18 61 views
1

嗨我正在使用jquery-cookies插件(http://plugins.jquery.com/project/Cookie),當我知道他們在做什麼時,設置和刪除cookie非常棒是。jQuery cookies和.each()幫助請求

我想完成的是讓我的腳本讀取所有當前存在的cookie,併爲每個存在的cookie執行一些操作。我現在使用看起來像這樣:

-

// set some dummy cookies 
$.cookie('cookie_name', 'cookie_value'); 
$.cookie('2', '2_value'); 
$.cookie('3', '3_value'); 
$.cookie('4', '4_value'); 

// this returns a "cookie_value" alert, so I know the above cookies are set and scripts are properly included: 
alert($.cookie('cookie_name')); // debug (working) 

// for each cookie found, check the input with the same id - then pop an alert, just to debug 
$.cookie(i,v).each(function(v) { 
    $('input#' + v + ').attr('checked', true); 
    alert(v); // debug (not working) 
}); 

-

...但是。每個語句不會做下蹲。這可能是語法問題,但是Cookie插件的文檔並未涵蓋這一點。有沒有人有任何想法?預先感謝您,請告訴我是否可以提供更多信息。

回答

2

你有多餘的話:

$('input#' + v + ').attr('checked', true); 
problem----------^ 

應該是:

$('input#' + v).attr('checked', true); 
+0

我喜歡你的指點了額外的報價,我會用這種方法在未來的格式化+1! :-) – 2014-06-18 15:58:35

0

這應該工作:

// set some dummy cookies 
$.cookie('cookie_name', 'cookie_value'); 
$.cookie('test', 'woot');  

$.each($.cookie(), function(i, v) { 
    alert(i + " = " + v); // outputs "cookie_name = cookie_value" etc. 
});