[英]check box change event not firing when checked property set from javascript
When I manually check/uncheck checkboxes, 当我手动选中/取消选中复选框时,
$('input[type=checkbox]').change(function(){})
fires perfectly. $('input[type=checkbox]').change(function(){})
完美触发。
But when I set the checked property inside code, this event handler is not get executed. 但是,当我在代码中设置checked属性时,不会执行此事件处理程序。
Markup: 标记:
<li><input id="p-Client-Management" type="checkbox" name="selectedMenuItem" value="Client Management">Client Management</input>
<ul>
<li><input class="parent-p-Client-Management" type="checkbox" name="selectedMenuItem" value="Demo Client">Demo Client</input></li>
<li><input class="parent-p-Client-Management" type="checkbox" name="selectedMenuItem" value="Basic Clients">Basic Clients</input></li>
</ul>
</li>
**Event handler :**
$('input[type=checkbox]').change(function(){
var id = $(this).attr('id');
if(id){
console.log("parent found -> "+id);
var children = $('.parent-'+id);
console.log("children of "+id+" -> "+children.length);
for(var i = 0; i<children.length; i++){
children[i].checked = document.getElementById(id).checked;
}
}
else{
if(this.checked){
console.log("Child is checked");
var suffix = $(this).attr('class').split("-")[2];
document.getElementById('p-'+suffix).checked = true;
}
});
I have id
for the parent checkbox and class
for child checkboxes. 我有父复选框的id
和子复选框的class
。 When parent checkbox is checked, the if
block will get executed showingconsole output parent found . 选中parent复选框时,将执行if
块,显示console output parent found 。 When child is checked, the else block will get executed showing console output Child is checked . 当选中child时,else块将被执行,显示控制台输出Checked 。 What I want is, on checking parent manually, the child's else
block will also get executed as their checked property is set with the parent and providing output Child is checked 我想要的是,在手动检查父对象时,子对象的else
块也将被执行,因为它们的检查属性是与父对象一起设置的,并提供了输出子对象被检查的功能
Programatically changing the value/checked status of an element won't trigger the change handler, but you can trigger them manually by using .trigger('change')
or by using the short hand .change()
以编程方式更改元素的值/检查状态不会触发更改处理程序,但是您可以使用.trigger('change')
或使用简写的.change()
来手动触发它们
$('input[type=checkbox]').change(function () {
var id = $(this).attr('id');
if (id) {
console.log("parent found -> " + id);
var children = $('.parent-' + id);
console.log("children of " + id + " -> " + children.length);
if (children.length) {
children[this.checked ? 'not' : 'filter'](':checked').prop('checked', this.checked).change()
}
} else {
if (this.checked) {
console.log("Child is checked");
var suffix = $(this).attr('class').split("-")[2];
$('#p-' + suffix).not(':checked').prop('checked', true).change();
}
}
});
Try this, here is the fiddle 试试这个,这是小提琴
$('input[type=checkbox]').unbind('change').change(function(){
var id = $(this).attr('id');
if(id=="p-Client-Management"){
console.log("parent found -> "+id);
var childrens = $('.parent-'+id);
var isChecked = $(this).is(':checked');
childrens.prop('checked', isChecked);
childrens.change();
} else{
console.log("Child is checked");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.