2014-03-27 130 views
0

如果語句檢查cookie是否存在以及它是true還是false,請檢查以下內容。由於某種原因,它無法正常工作。jQuery cookie if語句布爾值quandary

下面的代碼:

// the if statement 

    if($.cookie('style-widget-state') === true){ 
    $('.style-widget').addClass('style-toggle-show'); 
    }; 

// setting the cookie on a toggleClass method 

    $('.style-tab').on('click', function() { 
    $(".style-widget").toggleClass("style-toggle-show"); 
    $.cookie('style-widget-state', $('.style-widget').hasClass('style-toggle-show'), {expires:365, path: '/'}); 
    $(this).toggleClass("ai-cogs ai-arrow-left5"); 
    return false; 
    }); 

非常感激任何見解。

回答

1

在cookie中的值存儲爲一個字符串,所以使用===將失敗的比較,試圖

if($.cookie('style-widget-state') === 'true'){ 
} 

演示:Fiddle

+0

沒有意識到,cookie的值存儲在串...假定布爾人從來都不是字符串。非常感謝。 –