質問

いくチェックを入れたり、外イベント <input type="checkbox" /> とjQuery?

役に立ちましたか?

解決

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

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

編集:これを行うチェックボックスは、キーボードを使用してのように、クリック以外の理由で変更したときにキャッチしません。この問題を回避するには、changeclickinsteadに耳を傾けます。

、プログラムのチェックを外す/チェックについては、を見てみましょうなぜ私ではありませんチェックボックスの変更イベントがトリガですか

他のヒント

私たちは、入力チェックボックスに取り付けられた1つを持っている場合、クリックがラベルに影響を与えるのでしょうか?

私はそれが.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);
});

それとも、1つのチェックボックスと同じことを行うことができます:

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