首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要在jQuery中选中的默认复选框

要在jQuery中选中的默认复选框
EN

Stack Overflow用户
提问于 2022-04-26 16:27:00
回答 1查看 83关注 0票数 1

我正在处理一堆合并代码,我不能仅仅将“选中”添加到输入框中作为默认值。这一页上有很多事情,我不想破坏它。有三个函数处理复选框。

基本上,这就是它所做的:有多个扇区的复选框。CheckboxAll将只检查其部分中的所有复选框。

代码语言:javascript
复制
BlahA CheckboxAll
   checkbox1 checkbox2 checkbox3
BlahB CheckboxAll
   checkbox4 checkbox5 checkbox6 checkbox7
BlahC CheckboxAll
   checkbox8 checkbox9

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

代码语言:javascript
复制
  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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 12:43:02

如果所有的复选框都有一个CheckboxAll类:

代码语言:javascript
复制
  $('.CheckboxAll').prop("checked",true);

会让他们全部被检查。

如果您希望检查所有复选框,而不管是哪个类:

代码语言:javascript
复制
  $('input:checkbox').prop("checked",true);

将您的所有复选框都设置为“选中”,而不管是哪个类。

如果希望它们都默认加载,请查看document.ready函数:

代码语言:javascript
复制
$(document).ready(function() {
   $('.CheckboxAll').prop("checked",true); 
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72017415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档