jQuery checkbox(全选&取消)

html:

<input type="checkbox" id="checkAll"/>全选

<input type="checkbox"/>

<input type="checkbox"/>

<input type="checkbox"/>

<input type="checkbox"/>

--注:全选/取消全选

javascript:

$("#checkAll").change(function(){

  if($("#checkAll").attr("checked")){

    $("input[type='checkbox']").attr("checked","true");

  }else{

    $("input[type='checkbox']").each(function(){

      this.checked = false;

        });

  }

});

 

//当下面内容选项变化时,全选根据相应的变化

$("input[type='checkbox']").each(function(){

  $(this).change(function(){

    if(!this.checked){

                 //如果有一个没有选中,那么全选“取消”

      $("#checkAll").attr("checked", false);

    }

           if($("input[type='checkbox']:checked").not($("#checkAll")).size() == $("input[type='checkbox']").not($("#checkAll")).size() ){

      //如果所有选项全部勾选,那么勾上“全选”

         $("#checkAll").attr("checked", true);

    }

  });

});

posted @ 2012-10-25 14:24  琴子  阅读(350)  评论(0编辑  收藏  举报