要签入的默认复选框 jQuery

Default Checkboxes to Checked in jQuery

我正在处理一堆复杂的代码,我不能只将“选中”作为默认值添加到输入框。此页面中发生了很多事情,我不想破坏它。有 3 个函数可以处理复选框。

基本上这就是它的作用:有多个复选框部分。 CheckboxAll 将仅检查其部分中的所有复选框。

BlahA CheckboxAll
   checkbox1 checkbox2 checkbox3
BlahB CheckboxAll
   checkbox4 checkbox5 checkbox6 checkbox7
BlahC CheckboxAll
   checkbox8 checkbox9

如何以及在何处将 CheckboxAll 框默认选中?

  function SelectAllSystems(form)
    {
        if (form.SelectAll.checked)
        {
            <cfif TotSystems gt 10>
            if (confirm("\nWARNING. The processing of your application request is subject to delay if you select All Systems. Please be sure to select ONLY the systems that you need access to in order to expedite the processing of your application.\n\nClick OK to continue selection of All Systems.\nClick Cancel to select individual systems."))
            </cfif>
            {
              <cfloop index="x" from="1" to="#TotSystems#">
                    form.System#x#.checked = true;
              </cfloop>
            }
            <cfif TotSystems gt 10>
            else
            {
                form.SelectAll.checked = false
            }
            </cfif>
        }
        else
        {
             <cfloop index="x" from="1" to="#TotSystems#">
                 form.System#x#.checked = false;
             </cfloop>
        }
    }

  <!---
    -   parameters:     a_PdM:
    -                           int value containing the PdM ID that the systems are in
    -
    -   purpose:        If not all check boxes are checked, check them all. However, if
    -                   all the check boxes are checked, uncheck them all.
    --->
    function toggleAllSystemCheckBoxes(pdmID)
    {
        <!--- this function is bound to a click event so it checks the state of the input after the mouse-up event --->
        var $allPdM = $('#SelectAll_' + pdmID); //get the selectAll checkbox for the pdmID passed (output the selectAll_XXX ID when we render the page)
        var $pdmSystems = $('input[type="checkbox"].System_PdM' + pdmID); //get all of the systems associated to that pdmID (output the pdm_XXX class when we render the page)
        if($allPdM.is(':checked'))
        {
            $pdmSystems.attr('checked', 'checked');
        }//if
        else
        {
            $pdmSystems.removeAttr('checked');
        }//else
    }//toggleAllSystemCheckBoxes()

    <!---
    -   parameters:     a_PdMID:
    -                           int value containing the PdM ID that the systems are in
    -
    -   purpose:        When a system is checked or unchecked, this code runs to make sure
    -                   that the PdM check box is appropriately checked or unchecked
    ---->
    function updateSelectAll(pdmID)
    {
        var $allPdm = $('#SelectAll_' + pdmID); //get the selectAll checkbox for the pdmID passed (output the selectAll_XXX ID when we render the page)
        var $pdmSystems = $('input[type="checkbox"].System_PdM' + pdmID); //get all of the systems associated to that pdmID (output the pdm_XXX class when we render the page)
        $pdmSystems.each(function()
        {
            if(!$(this).is(':checked'))
            {
                $allPdm.removeAttr('checked');
                return false;
            }//if
            else
            {
                $allPdm.attr('checked', 'checked');
            }//else
        })
    }//updateSelectAll()

如果所有复选框都有 class 个 CheckboxAll:

  $('.CheckboxAll').prop("checked",true);

将全部设置为选中。

如果您希望选中所有复选框而不考虑 class:

  $('input:checkbox').prop("checked",true);

将设置所有复选框,无论 class 是否选中。

如果您希望它们在加载时全部默认,请查看 document.ready 函数:

$(document).ready(function() {
   $('.CheckboxAll').prop("checked",true); 
});