jquery checkbox 全选和反选

两种方法,第一种用jquery的toggle,但这种方法有瑕疵,其它的倒是全选了,可它自己本是却不能选择...只能用第二种方法bind一个click事件,然后判断了

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(function(){
      //方法一
      /*
      $("#checkAll").toggle(
         function(){
           $.each($("input[type='checkbox']"), function(){$(this).attr("checked",true);},
         function(){$.each($("input[type='checkbox']"), function(){$(this).attr("checked",false);}
       );
       */
      //方法二
      $("#checkAll").bind("click",function(){
       if($("#checkAll").attr("checked")){
           $("#margin_total").hide();
           $.each($("input[type='checkbox']"), function(){$(this).attr("checked",true);});
       }else{
           $("#margin_total").show();
           $.each($("input[type='checkbox']"), function(){$(this).attr("checked",false);});
       }
      })
})

</script>
<html>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
       <th><input id="checkAll" type="checkbox" /></th>
       <th> - </th>
       <th> - </th>
   </tr>
   <tr>
      <td><input type="checkbox" /></td>
      <td> - </td>
      <td> - </td>
   </tr>
   <tr>
      <td><input type="checkbox" /></td>
      <td> - </td>
      <td> - </td>
   </tr>
   <tr>
      <td><input type="checkbox" /></td>
      <td> - </td>
      <td> - </td>
   </tr>
</table>
</body>
</html>