简体   繁体   English

JavaScript Checkbox.OnChange事件无法正确触发

[英]Javascript Checkbox.OnChange event not firing properly

So I have a business case where I've got groups (called Bundles) and they can contain other Bundles. 因此,我有一个业务案例,其中有组(称为捆绑软件),它们可以包含其他捆绑软件。 Now, in my interface, I'm trying to have it so when you check the top level, it automatically checks (and disables, but I'm not there yet) the child bundles. 现在,在我的界面中,我正在尝试使用它,因此当您检查顶层时,它会自动检查(并禁用,但我还没有)子束。

In order to accomplish that, each checkbox has an onchange event where it passes in this and a comma-delimited list of other Bundles that should be checked. 为了做到这一点,每个复选框都有一个onchange事件,在this事件中传递this事件以及应检查的其他Bundle的逗号分隔列表。 The code I've pasted below behaves unexpectedly. 我在下面粘贴的代码的行为异常。 Namely, when I'm forcing the Change event to be called (because programatically manipulating the checked state doesn't raise the Change event) the subsequent call winds up using the exact same arguments that the original Change event used. 即,当我强制调用Change事件(因为以编程方式操纵选中状态不会引发Change事件)时,随后的调用将使用与原始Change事件完全相同的参数结束。

It's almost as if when I call .change() , it's passing my original checkbox (not the cascaded one) and original list of children. 几乎就像我调用.change() ,它传递了我的原始复选框(而不是层叠的复选框)和原始的子代列表。

I've put in a ton of alerts, and the clutch one says "about to trigger change for [the right checkbox]", but then the very next alert says "disable raised for [the original/wrong checkbox]". 我已经发出大量警报,离合器中的一句话是“即将触发[正确的复选框]的更改”,但随后的下一个警报显示为“为[原始/错误的复选框]禁用了警告”。

Any ideas on why programmatically raising the Change event is causing its arguments to be all messed up? 关于为什么以编程方式引发Change事件导致其参数全部混乱的任何想法?

function disableChildren(chkBundle, childBundles) {

        var bundleId = chkBundle.id.substr(chkBundle.id.lastIndexOf("chk"));
        alert("disable raised for '" + bundleId + "' using children '" + childBundles + "'");
        jQuery("#BundleList input:checkbox[id*=" + bundleId + "id]").attr("checked", chkBundle.checked);

        var childIds = childBundles.split(",");

        for (var i = 0; i < childIds.length; i++) {
            jQuery("#BundleList input:checkbox[id$=chk" + childIds[i] + "]").each(function(index, domEle) {
                if (domEle.checked != chkBundle.checked) {
                    alert('about to check ' + domEle.id);
                    domEle.checked = chkBundle.checked;
                    alert('about to trigger change for ' + domEle.id);
                    domEle.change();
                    alert('done triggering ' + domEle.id);
                }
            });
        }
    }

And an example of one of the html for a checkbox: 还有一个复选框的html示例:

<input id="BundleAssignment_rptMainBundle_ctl02_chk1" 
type="checkbox" name="BundleAssignment$rptMainBundle$ctl02$chk1" 
onchange="disableChildren(this,'7,8')" onclick="disableChildren(this,'7,8')" />

remove anything to do with change . 删除与change任何东西。 replace domEle.change(); 替换domEle.change(); with domEle.click(); domEle.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM