用jQuery全选或不全选checkBox



用jQuery全选或不全选checkBox 。网上看了很多checkbox的全选功能,但都需要做一定的修改,且用来全选的checkbox没控制,下面这个不用做任何修改,只要html中全选的checkbox的ID与其他的checkbox的class保持一样即可,如:

<tr id=”first-tr”>

<td width=”5%”><input type=”checkbox” id=”sceneryCheckbox“></td>

 </tr>

<tr>

<td><input type=”checkbox” /></td>

</tr>

/**
* checkBox的全选和全中选
*/
$(document).ready(function() {
 $(‘[type=checkbox]‘).click(function (){
  var checkClass = $(this).attr(“class”);
  if(checkClass){
   if($(“.”+checkClass).is(“:checked”)){
    $(“#”+checkClass).attr(“checked”,”checked”);
   }else{
    $(“#”+checkClass).removeAttr(“checked”);
   }
  }
  var checkID = $(this).attr(“id”);
  if(checkID){
   if($(“#”+checkID).is(“:checked”)){
    $(“.”+checkID).each(function (){
     $(this).attr(“checked”,”checked”);
    });
   }else{
    $(“.”+checkID).each(function (){
     $(this).removeAttr(“checked”);
    });
   }
  }
 });
});