2014-08-29 72 views
0

我需要帮助,我有一个脚本,保持由jquery cookie的更改,如果我重新加载它的工作页面,但如果刷新div不工作了。 我错在哪里?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()的问题。

+0

您正在将cookie中的布尔值“true”存储在cookie中,但是当您读取cookie时,您将它与字符串“true”进行比较。 – Barmar 2014-08-29 11:06:14

回答

0

变化:

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

只是

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

你不应该用字符串"true" cookie的比较,因为你不是存储在cookie的字符串,你存储布尔型truefalse

+0

谢谢,但在div刷新后它不起作用。没有问题,如果我重新加载页面。 – Simon 2014-08-29 12:56:58

+0

你是什么意思的div刷新?你的意思是什么'agg_menu'每10秒做一次? – Barmar 2014-08-29 12:57:43