捕获选中的复选框更改事件

时间:2021-08-06 23:59:55

How do I to catch check/uncheck event of <input type="checkbox" /> with jQuery?

如何用jQuery捕获的check/uncheck事件?

9 个解决方案

#1


136  

<input type="checkbox" id="something" />

$("#something").click( function(){
   if( $(this).is(':checked') ) alert("checked");
});

Edit: Doing this will not catch when the checkbox changes for other reasons than a click, like using the keyboard. To avoid this problem, listen to changeinstead of click.

编辑:当复选框由于其他原因而改变时,如使用键盘,这样做将不会被捕获。为了避免这个问题,请听change而不是click。

For checking/unchecking programmatically, take a look at Why isn't my checkbox change event triggered?

对于以编程方式检查/取消检查,请查看为什么我的复选框更改事件没有被触发?

#2


36  

The click will affect a label if we have one attached to the input checkbox?

如果我们将一个标签附加到输入复选框中,那么点击将会影响一个标签?

I think that is better to use the .change() function

我认为最好使用.change()函数

<input type="checkbox" id="something" />

$("#something").change( function(){
  alert("state changed");
});

#3


19  

Use the :checked selector to determine the checkbox's state:

使用:勾选选择器确定复选框的状态:

$('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
        ...
    } else {
        ...
    }
});

#4


11  

For JQuery 1.7+ use:

JQuery 1.7 +使用:

$('input[type=checkbox]').on('change', function() {
  ...
});

#5


4  

Use below code snippet to achieve this.:

使用下面的代码片段来实现这一点。

$('#checkAll').click(function(){
  $("#checkboxes input").attr('checked','checked');
});

$('#UncheckAll').click(function(){
  $("#checkboxes input").attr('checked',false);
});

Or you can do the same with single check box:

或者你也可以用一个复选框:

$('#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'); 
  }
});

For demo: http://jsfiddle.net/creativegala/hTtxe/

演示:http://jsfiddle.net/creativegala/hTtxe/

#6


3  

In my experience, I've had to leverage the event's currentTarget:

根据我的经验,我必须利用事件的当前目标:

$("#dingus").click( function (event) {
   if ($(event.currentTarget).is(':checked')) {
     //checkbox is checked
   }
});

#7


2  

use the click event for best compatibility with MSIE

使用单击事件以获得与MSIE的最佳兼容性

$(document).ready(function() {
    $("input[type=checkbox]").click(function() {
        alert("state changed");
    });
});

#8


1  

This code does what your need:

此代码可以满足您的需要:

<input type="checkbox" id="check" >check it</input>

$("#check").change( function(){
   if( $(this).is(':checked') ) {
        alert("checked");
    }else{
        alert("unchecked");
   }
});

Also, you can check it on jsfiddle

而且,您可以在jsfiddle检查它

#9


-1  

$(document).ready(function(){
    checkUncheckAll("#select_all","[name='check_boxes[]']");
});

var NUM_BOXES = 10;
// last checkbox the user clicked
var last = -1;
function check(event) {
  // in IE, the event object is a property of the window object
  // in Mozilla, event object is passed to event handlers as a parameter
  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;
}
function init() {
  for (var i = 0; i < NUM_BOXES; i++)
    document.forms.boxes['box[' + i + ']'].onclick = check;
}

HTML:

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>

#1


136  

<input type="checkbox" id="something" />

$("#something").click( function(){
   if( $(this).is(':checked') ) alert("checked");
});

Edit: Doing this will not catch when the checkbox changes for other reasons than a click, like using the keyboard. To avoid this problem, listen to changeinstead of click.

编辑:当复选框由于其他原因而改变时,如使用键盘,这样做将不会被捕获。为了避免这个问题,请听change而不是click。

For checking/unchecking programmatically, take a look at Why isn't my checkbox change event triggered?

对于以编程方式检查/取消检查,请查看为什么我的复选框更改事件没有被触发?

#2


36  

The click will affect a label if we have one attached to the input checkbox?

如果我们将一个标签附加到输入复选框中,那么点击将会影响一个标签?

I think that is better to use the .change() function

我认为最好使用.change()函数

<input type="checkbox" id="something" />

$("#something").change( function(){
  alert("state changed");
});

#3


19  

Use the :checked selector to determine the checkbox's state:

使用:勾选选择器确定复选框的状态:

$('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
        ...
    } else {
        ...
    }
});

#4


11  

For JQuery 1.7+ use:

JQuery 1.7 +使用:

$('input[type=checkbox]').on('change', function() {
  ...
});

#5


4  

Use below code snippet to achieve this.:

使用下面的代码片段来实现这一点。

$('#checkAll').click(function(){
  $("#checkboxes input").attr('checked','checked');
});

$('#UncheckAll').click(function(){
  $("#checkboxes input").attr('checked',false);
});

Or you can do the same with single check box:

或者你也可以用一个复选框:

$('#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'); 
  }
});

For demo: http://jsfiddle.net/creativegala/hTtxe/

演示:http://jsfiddle.net/creativegala/hTtxe/

#6


3  

In my experience, I've had to leverage the event's currentTarget:

根据我的经验,我必须利用事件的当前目标:

$("#dingus").click( function (event) {
   if ($(event.currentTarget).is(':checked')) {
     //checkbox is checked
   }
});

#7


2  

use the click event for best compatibility with MSIE

使用单击事件以获得与MSIE的最佳兼容性

$(document).ready(function() {
    $("input[type=checkbox]").click(function() {
        alert("state changed");
    });
});

#8


1  

This code does what your need:

此代码可以满足您的需要:

<input type="checkbox" id="check" >check it</input>

$("#check").change( function(){
   if( $(this).is(':checked') ) {
        alert("checked");
    }else{
        alert("unchecked");
   }
});

Also, you can check it on jsfiddle

而且,您可以在jsfiddle检查它

#9


-1  

$(document).ready(function(){
    checkUncheckAll("#select_all","[name='check_boxes[]']");
});

var NUM_BOXES = 10;
// last checkbox the user clicked
var last = -1;
function check(event) {
  // in IE, the event object is a property of the window object
  // in Mozilla, event object is passed to event handlers as a parameter
  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;
}
function init() {
  for (var i = 0; i < NUM_BOXES; i++)
    document.forms.boxes['box[' + i + ']'].onclick = check;
}

HTML:

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>