无法删除jquery cookie

时间:2011-07-19 01:47:28

标签: jquery cookies

我正在使用jquery cookie来传递我在第一页上点击的元素的值,以便在下一页上使用。我遇到的问题是,每当我将cookie设置为null时,该值都不会删除。它仍然存在。

我的第一页上的jquery脚本

<script>
$(document).ready(function() {
    $("div.product-header").click(function() {
        var index = $("div.product-header").index(this);
        $.cookie("product_name", index);
        //alert("product category: "+$.cookie("product_name"));
    });
    $("div.product-subheader").click(function() {
        var index = $("div.product-subheader").index(this);
        $.cookie("product_subheader", index);
        //alert("product category item: "+$.cookie("product_subheader"));
    });
});
</script>

将使用cookie的第二页脚本(在此页面上cookie正常工作)

<script>
$(document).ready(function () {
    $(".product-contents").hide();
    $('div.product-header').eq($.cookie('product_name')).addClass('active').next().show(); 
    $('div.product-subheader').eq($.cookie('product_subheader')).css({fontWeight: 'bold', backgroundColor: '#eeeeee'});             
    $('div.product-header').click(function(){
       $.cookie('product_name',$('div.product-header').index(this));
        if( $(this).next().is(':hidden') ) { 
            $('div.product-header').removeClass('active').next().hide(); 
            $(this).toggleClass('active').next().show();
        }
        return false; 
    });
});
</script>

但是当我尝试在第二页上使用脚本时;每当我使用 $ .cookie(“product_name”,null); $ .cookie(“product_subheader”,null); cookie时,cookie都不会被删除没有删除

<script>
$(document).ready(function() {
    $("div.product-header").click(function() {
        $.cookie("product_name", null);
        alert("cookie product category should be null not: "+$.cookie("product_name"));

        var index = $("div.product-header").index(this);
        $.cookie("product_name", index);
    });

    $("div.product-subheader").click(function() {
        $.cookie("product_subheader", null);
        alert("cookie product category item should be null not: "+$.cookie("product_subheader"));   

        var index = $("div.product-subheader").index(this);
        $.cookie("product_subheader", index);
    });
});
</script>

我上面代码的任何更正?

1 个答案:

答案 0 :(得分:5)

您必须在设置cookie时指定路径。使用

 $.cookie("product_name", index, { path: '/' });

这应该允许您从创建cookie的页面以外的页面中删除它。

相关问题