我試圖理解爲什麼在下面的例子中更改事件不會被觸發(我會告訴確切位置)。如果更改來源不是來自html的話,那麼javascript事件不會被觸發?
我有一個複選框,讓我們把它叫做「mainCheckbox」,選中後 - 我要檢查一些其他相關的複選框(到目前爲止工作)。
此外,當我取消了相關的複選框(子複選框)中的一個 - 我要取消的mainChecbox,這也適用 - 但這裏是什麼我不理解:
我改變checked屬性在mainCheckbox(在「childCheckbox」的onChange處理)的
怎麼就mainCheckbox的onChange處理不會被調用?
或者主複選框的onchange事件沒有被觸發?
這裏是代碼:
//binding to the 'mainCheckbox' change event:
$("[data-role='group']").bind("change", function(event){
//checking/unchecking all related checkboxes respectivly
$("li input[data-group='" + event.target.id + "'").prop('checked', $(this).prop("checked"));
})
//binding to the change event of every 'child checkbox'
$("li input[data-group]").bind("change", function(){
if (event.target.checked){
//if the child checkbox is now checked - I am checking if now all child checkboxes are checked,
//if so - I need to check the main checkbox.
if ($("[data-group=" + event.target.dataset.group + "]:checked").length == $("[data-group=" + event.target.dataset.group + "]").length){
$("#" + event.target.dataset.group).prop("checked", true);
}
//TODO: add this device to the selectedDevices array.
}
else {
//the checkbox is now unchecked - so I am unchecking the main checkbox -
//but here is my failing to understand part: I am unchecking the main checkbox -
//why the change event is not triggered? I thought that now all child checkboxes will be unchecked as well
//(I am happy they are not :) just failing to understand why)...
$("#" + event.target.dataset.group).prop("checked", false);
//TODO: remove this device from the selectedDevices array.
}
})
在此先感謝所有的反應,
吉米
您是否在更改處理程序中更改主複選框的「單擊」屬性?這是一個錯字嗎? – marekful
錯字!編輯。 – JimmyBoy