捕获checkbox的更改事件
如何使用jQuery捕获/取消选中<input type="checkbox" />事件? 
 <input type="checkbox" id="something" /> $("#something").click( function(){ if( $(this).is(':checked') ) alert("checked"); }); 
 编辑:这样做不会赶上当checkbox更改为其他原因,而不是点击,如使用键盘。 为了避免这个问题,听取change而不是click 。 
为了检查/取消选中编程,看看为什么不是我的checkbox更改事件触发?
点击会影响一个标签,如果我们有一个附加到inputcheckbox?
我认为最好使用.change()函数
 <input type="checkbox" id="something" /> $("#something").change( function(){ alert("state changed"); }); 
使用:checkedselect器来确定checkbox的状态:
 $('input[type=checkbox]').click(function() { if($(this).is(':checked')) { ... } else { ... } }); 
对于JQuery 1.7+使用:
 $('input[type=checkbox]').on('change', function() { ... }); 
根据我的经验,我不得不利用事件的currentTarget:
 $("#dingus").click( function (event) { if ($(event.currentTarget).is(':checked')) { //checkbox is checked } }); 
使用下面的代码片段来实现这一点:
 $('#checkAll').click(function(){ $("#checkboxes input").attr('checked','checked'); }); $('#UncheckAll').click(function(){ $("#checkboxes input").attr('checked',false); }); 
或者你也可以用单个checkbox来做同样的事情:
 $('#checkAll').click(function(e) { if($('#checkAll').attr('checked') == 'checked') { $("#checkboxes input").attr('checked','checked'); $('#checkAll').val('off'); } else { $("#checkboxes input").attr('checked', false); $('#checkAll').val('on'); } }); 
对于演示: http : //jsfiddle.net/creativegala/hTtxe/
使用click事件与MSIE的最佳兼容性
 $(document).ready(function() { $("input[type=checkbox]").click(function() { alert("state changed"); }); }); 
这段代码可以满足你的需要:
 <input type="checkbox" id="check" >check it</input> $("#check").change( function(){ if( $(this).is(':checked') ) { alert("checked"); }else{ alert("unchecked"); } }); 
另外,你可以在jsfiddle上检查它
  $(document).ready(function(){ 
  $(document).ready(function(){ 
checkUncheckAll( “#SELECT_ALL”, “[名= 'check_boxes []']”);
});
  var NUM_BOXES = 10;
 / /最后一个checkbox用户点击
 var last = -1;
function检查(事件){
   //在IE中,事件对象是窗口对象的一个属性
   //在Mozilla中,事件对象作为parameter passing给事件处理程序
   if(!event){event = window.event}
   var num = parseInt(/ box [(\ d +)] / .exec(this.name)[1]);
   if(event.shiftKey && last!= -1){
     var di = num> last?  1:-1;
     for(var i = last; i!= num; i + = di){
       document.forms.boxes ['box ['+ i +']']。checked = true;
     }
   }
   last = num;
 }
函数init(){
   for(var i = 0; i <NUM_BOXES; i ++){
     document.forms.boxes ['box ['+ i +']']。onclick = check;
   }
 } 
HTML代码
 <body onload =“init()”>
 <form name =“boxes”>
 <input name =“box [0]”type =“checkbox”>
 <input name =“box [1]”type =“checkbox”>
 <input name =“box [2]”type =“checkbox”>
 <input name =“box [3]”type =“checkbox”>
 <input name =“box [4]”type =“checkbox”>
 <input name =“box [5]”type =“checkbox”>
 <input name =“box [6]”type =“checkbox”>
 <input name =“box [7]”type =“checkbox”>
 <input name =“box [8]”type =“checkbox”>
 <input name =“box [9]”type =“checkbox”>
 </ FORM> </ BODY>