Pergunta

Como faço para verificação catch / evento desmarque de <input type="checkbox" /> com jQuery?

Foi útil?

Solução

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

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

Edit: Isso não vai pegar quando a caixa de seleção muda por outras razões que um clique, como o uso do teclado. Para evitar esse problema, ouvir changeinstead de click.

Para marcando / desmarcando programaticamente, dê uma olhada Por que não é minha alteração caixa de seleção evento disparado?

Outras dicas

O clique afetará um rótulo, se temos um ligado à caixa de entrada?

Eu acho que é melhor usar a função .change ()

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

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

Use a : verificada selector para determinar o estado da caixa de seleção:

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

Para JQuery 1.7+ uso:

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

Use abaixo trecho de código para conseguir isso:.

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

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

Ou você pode fazer o mesmo caixa de seleção única com:

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

Para demonstração: http://jsfiddle.net/creativegala/hTtxe/

Na minha experiência, eu tive que aproveitar currentTarget do evento:

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

usar o evento clique para melhor compatibilidade com MSIE

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

Este código faz o que a sua necessidade:

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

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

Além disso, você pode verificá-lo na 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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top