我如何赶上检查/ <input type="checkbox" />的取消选中事件与jQuery?

有帮助吗?

解决方案

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

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

编辑:这样做时复选框其他原因比点击变化,比如使用键盘不会赶上。为了避免这个问题,听changeinstead click的。

有关检查/编程取消选中,看看为什么不是我的复选框变化事件触发?

其他提示

的点击将影响一个标签,如果我们有一个连接到输入复选框?

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

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

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

使用:检查选择器,以确定该复选框的状态:

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

有关的JQuery 1.7+使用:

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

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

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

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

或者你也可以做同样的单复选框:

$('#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/

在我的经验,我已经利用该事件的currentTarget当前:

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

使用点击事件与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(){
    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:

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top