25

jQuery を使用してチェックされたボックスは、ハンドラーprop()にアタッチされたリスナーには影響しません。change

私のコードは次のようなものです

HTML

<div>
    <label>
        <input type="checkbox" class="ch" />test</label>
    <label>
        <input type="checkbox" class="ch" />test</label>
    <label>
        <input type="checkbox" class="ch" />test</label>
    <input type="button" value="check the box" id="select" />
</div>

JS

 $("body").on("change", ".ch", function(){

  alert("checked");

});


$("body").on("click", "#select", function(){

  $(this).parent("div").find("input[type=checkbox]").prop("checked", true);

});

チェックボックスをクリックするとアラートが発生します。チェックボックスのプロパティが変更されたときに起動するにはどうすればよいですか? JSBIN

4

3 に答える 3

7

関数内からイベントをトリガーします。

$("body").on("click", "#select", function(){
  $(this).parent("div").find("input[type=checkbox]").prop("checked", true).trigger("change");
});
于 2013-10-21T21:26:56.907 に答える