在Div刷新中丢失了Jquery Cookie

时间:2014-08-29 10:51:08

标签: jquery each setinterval jquery-cookie

我需要帮助,我有一个脚本可以通过jquery cookie进行更改,如果我重新加载它工作的页面,但是如果刷新div不再工作了。 我哪里错了?

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="scripts/jquery.cookie.js"></script>
<style>
table {
    border: 1px solid #999;
}
#refresh{
    text-align:center;
}
#counter{
    text-align:center;
}
</style>
</head>

<body>
<div id="counter">0
</div>
<div id="container">
    <div id="refresh">
        <table cellpadding="0" cellspacing="5" width="100%" height="60%">
                <tr>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="1" /></td>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="2" /></td>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="3" /></td>
                </tr>
                <tr>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="4" /></td>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="5" /></td>
                    <td width="20%" height="20%"><input class="box" type="checkbox" name="6" /></td>
                </tr>
            </table>
    </div>
</div>
<script>
var isScrolling = false;
var lastScrollPos = 0;
var counter = 1;

$(function() {

    $('#refresh').on('scroll', function() {
        isScrolling = true;
        lastScrollPos = this.scrollTop;
    });

    refreshTimer = setInterval(agg_menu, 10000);

    var updateTotal = function(){
        $("input.box").each(function() {

            var mycookie = $.cookie($(this).attr('name'));
            if (mycookie && mycookie == "true") {
                $(this).prop('checked', mycookie);
            }
        });
        $("input.box").change(function() {
            $.cookie($(this).attr("name"), $(this).prop('checked'), {
                expires: 365
            });
        });
    }

    updateTotal();

    function agg_menu(){
        if (!isScrolling) {
            var referer = 'pppp.php #refresh';
            $("#container").load(referer);
            $("#counter").text(counter++);
            updateTotal();
        }
        isScrolling = false;
    }   
});
</script>

我不确定它是否是jquery cookie或函数each()的问题。

1 个答案:

答案 0 :(得分:0)

变化:

        if (mycookie && mycookie == "true") {
            $(this).prop('checked', mycookie);
        }

        $(this).prop('checked', mycookie);

您不应该将Cookie与字符串"true"进行比较,因为您没有在Cookie中存储字符串,而是存储布尔值true或{ {1}}。