本文介绍了用prop()选中的复选框不会触发附加到& quot; change& quot;的事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
框不会影响附加到 change
处理程序的侦听器.
boxes checked using jQuery prop()
do not affect listeners attached to change
handler.
我的代码类似于
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
the alert fires when I click on the checkbox. How can I make it fire when the property of the checkbox changes? JSBIN
推荐答案
您必须使用 .change()> 触发更改事件侦听器:
You have to use .change() to trigger the change event listener:
$("body").on("change", ".ch", function () {
alert("checked");
});
$("body").on("click", "#select", function () {
$(this).parent("div").find("input[type=checkbox]").prop("checked", true).change();
});
JSBbin 或小提琴
请注意,这将引发许多事件.jsBin中的html示例中的三个.
JSBbin or Fiddle
Please note that this will fire many events. Three in the html example you have in jsBin.
这篇关于用prop()选中的复选框不会触发附加到& quot; change& quot;的事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!