简体   繁体   English

选中/取消选中主复选框时,触发其他复选框onclick()

[英]Fire other checkboxes onclick() when main checkbox is selected/unselected

I have read many other question on SO regarding selecting all checkboxes, but couldn't find one that says how to trigger other checkboxes actions. 我已经阅读了关于选择所有复选框的许多其他问题,但是找不到表明如何触发其他复选框动作的问题。

I have a main checkbox, when this checkbox is selected all other checkboxes are selected as well. 我有一个主复选框,当选中此复选框时,还将选中所有其他复选框。 The thing is, other checkboxes have to perform their own action eg disable text fields .etc if they are checked/unchecked. 问题是,其他复选框必须执行自己的操作,例如,如果选中/取消选中,则禁用文本字段.etc。 I achieved this by adding onclick to each checkbox. 我通过向每个复选框添加onclick实现了这一点。 So how to call each checkbox onclick (or make each checkbox to perform their action) if the main checkbox is selected? 那么,如果选择了主复选框,那么如何调用每个复选框的onclick(或使每个复选框执行其操作)?

After reading about this I have made the following: 阅读此内容后,我做了以下工作:

      // main checkbox functin that checks/unchecks other checkboxes. 

  function toggleAllCheckBoxes(source , operationType) {
                checkboxes = document.getElementsByName("checkBox" + operationType);
                for each(var checkbox in checkboxes)
                checkbox.checked = source.checked;
               // checkbox.addEventListener('click', true); // this is wrong.
            }


  //this will toggle text fields enabled/disabled
 function toggleDisable(oldTable, newTable , checkBox)
    {
        if( document.getElementById(newTable) !=null)// in case to toggle only old table
        {
            document.getElementById(newTable).disabled = checkBox.checked;
        }

        document.getElementById(oldTable).disabled = checkBox.checked;
    }

main checkbox: 主要复选框:

All<input type="checkbox" onClick="toggleAllCheckBoxes(this , '<?=$operationType?>')"/> 

Other checkboxes 其他复选框

  X<input name="checkbox" id="checkbox" onclick="toggleDisable('<?= $originalTableId?>' , '<?= $backupTableId?>' , this )" type="checkbox" /> 

So far this works fine, except, text fields are not being enabled/disabled if main checkbox is selected. 到目前为止,此方法工作正常,但如果选中了主复选框,则不会启用/禁用文本字段。 Although single checkboxes are able to do that. 尽管单个复选框能够做到这一点。

Thanks. 谢谢。

I see two issues: 我看到两个问题:

  1. There is no for each operator in JavaScript. JavaScript中没有for each运算符。 You'll need to use a normal for loop here. 您需要在此处使用普通的for循环。
  2. You don't want to set the checked attribute for the checkbox because, as you've seen, that will bypass all event handlers. 您不想设置复选框的checked属性,因为如您所见,它将绕过所有事件处理程序。 Instead, call click() on the checkbox and it will be checked (or unchecked) and all event handlers will be called. 相反,在复选框上调用click() ,它将被选中(或取消选中),并将调用所有事件处理程序。

That will end up looking like this in toggleAllCheckBoxes : 这样最终会在toggleAllCheckBoxes看起来像这样:

for(var i = 0; i < checkboxes.length; i++)
    checkboxes[i].click();

Here's a Fiddle to try this code out on: http://jsfiddle.net/z4Ltc/ 这是一个小提琴手,可以尝试以下代码: http : //jsfiddle.net/z4Ltc/

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

相关问题 复选框onclick取消选中其他复选框 - Checkbox onclick uncheck other checkboxes AngularJs将在所有其他复选框上切换选中的复选框 - AngularJs Checkbox that will toggle selected on all other checkboxes 复选框的奇怪的onclick行为。 选中复选框后尝试写入浏览器控制台 - Strange onclick behavior for checkboxes. Trying to write onto browser console when a checkbox item is selected 解释javascript中的程序逻辑,以在选中所有其他复选框时选中一个复选框 - Explain the program logic in javascript to make a single checkbox selected when all other checkboxes are selected 如何使不确定复选框触发其他复选框的功能? - How to get Indeterminate Checkbox to fire function of other checkboxes? 显示选中的区域中的复选框,未选中的区域中的复选框 - Display the checkboxes selected into a section and the unselected into another one 选中一个父复选框时禁用所有其他顶级复选框 - Disable all other top level checkboxes when one parent checkbox is selected 选中复选框后,取消选中页面上的其他复选框 - Uncheck other checkboxes on page when checking a checkbox Angularjs - 复选框 - 计算多个选中/未选中的项目 - Angularjs - Checkbox - count multiple selected/unselected items jQuery-如果用户选择了特定复选框,请取消选中其他复选框 - jQuery - Uncheck other checkboxes if a specific checkbox is selected by user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM