全选/取消全选jq

<script src="jquery-1.6.2.min.js"></script>


<input type="checkbox" id="ckAll" />check all<br />
<input type="checkbox" name="sub" />1<br />
<input type="checkbox" name="sub"/>2<br />
<input type="checkbox" name="sub"/>3<br />
<input type="checkbox" name="sub"/>4<br />


<script>

  $("#ckAll").click(function() {
    $("input[name='sub']").prop("checked", this.checked);
  });
  
  $("input[name='sub']").click(function() {
    var $subs = $("input[name='sub']");
    $("#ckAll").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
  });

</script>

posted on 2014-12-03 14:58  fromIRIS  阅读(180)  评论(0)    收藏  举报