使用prop()检查复选框不会触发附加到“更改”的事件。 [英] checkbox checked with prop() does not fire events attached to "change"
本文介绍了使用prop()检查复选框不会触发附加到“更改”的事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery prop()
检查的框不会影响附加到更改
处理程序的侦听器。
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()检查复选框不会触发附加到“更改”的事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文