jquery 根据cookie值选中checkbox

1泡泡1 发布于 2013/05/16 14:47
阅读 2K+
收藏 0
代码基本实现了刷新后不丢失选中的图片,但是让checkbox也是选中的状态啊?求指教
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
#www {
	position:absolute;
	left:1022px;
	top:2px;
	width:100px;

	z-index:1;

}
-->
</style>
</head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>

	<script language="javascript" type="text/javascript">
	
	$(document).ready(function(){
	var str = $.cookie("zzz");
	var strArray = str.split(",");
	for(var i=0;i<strArray.length;i++){  
		  //alert(strArray[i]);
		 
		  $("#www").append("<img src="+strArray[i]+" style='width:100px; height:69px;'>");
     $("#www").append("<span>"+strArray[i]+"</span>");
		  }	
		
		
       $(".js").click(function(){
                   var aa="";
                   var bb="";
                   $('#www').empty();
                               $("input[name=checkbox1]").each(function(){
                    	         if(this.checked){
                             aa=this.value;
                               bb+=this.value+",";                                                                                            
                                    // aa+=this.value+",";
                            }
                            })
                    var cc= bb.substring(0,bb.length-1) ;
                       $.cookie("zzz", cc,{expires:1});  
                          // alert($.cookie("zzz"));  
                            //$("#www1").text(bb);
                            
                            var str = $.cookie("zzz");
	var strArray = str.split(",");
	for(var i=0;i<strArray.length;i++){  
		  //alert(strArray[i]);
		  $("#www").append("<img src="+strArray[i]+" style='width:100px; height:69px;'>");
                     $("#www").append("<span>"+strArray[i]+"</span>");
		  }
                            
                            
        })
})

		
		</script>
<body>
<table width="485" height="27" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <th width="159" scope="col"><img src="1.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="2.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="3.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="4.jpg" width="100" height="69" /></th>
  </tr>
  <tr class="js">
    <th scope="col"><input type="checkbox" name="checkbox1"  value="1.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="2.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="3.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="4.jpg" ></th>
  </tr>
  
  <tr>
    <th width="159" scope="col"><img src="1.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="2.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="3.jpg" width="100" height="69" /></th>
    <th width="159" scope="col"><img src="4.jpg" width="100" height="69" /></th>
  </tr>
  <tr class="js">
    <th scope="col"><input type="checkbox" name="checkbox1"  value="1.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="2.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="3.jpg" ></th>
    <th scope="col"><input type="checkbox" name="checkbox1"  value="4.jpg" ></th>
  </tr>
  <div id="www"></div>
    <div id="www1"></div>
</table>
</body>
</html>

 

加载中
0
sjunjun
sjunjun

你的代码太乱了,看着费劲

$checkbox.attr({checked:'checked'});//选中

$checkbox.removeAttr('checked');//取消选中

苏生不惑
苏生不惑
prop怎么用,都没用过这个。。
Tuesday
Tuesday
attr会有兼容问题, 建议用prop
返回顶部
顶部