【问题标题】:CheckBoxList Items using JavaScript使用 JavaScript 的 CheckBoxList 项目
【发布时间】:2011-04-28 16:36:26
【问题描述】:

我有一个包含几个项目和一个全部选项的检查表。用户可以全选,我希望它检查所有选项,如果他们取消选中所有选项,它将取消选中所有选项。

我已经用下面的代码完成了。

<script language="javascript" type="text/javascript">
    function CheckBoxListSelect(cbControl) //, state)
    {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");

        alert(chkBoxCount[0].checked);

        for(var i=0;i<chkBoxCount.length;i++)
        {   
            chkBoxCount[i].checked = chkBoxCount[0].checked //state;
        }

        return false; 
    }
</script>

cblAffiliation.Attributes.Add("onclick", "javascript: CheckBoxListSelect ('" & cblAffiliation.ClientID & "');")

问题是,如果我选择任何一个框,它会循环遍历,然后将它们设置为所有选项。我无法找出解决此问题的最佳方法。

我想避免在复选框列表旁边使用复选框,然后我必须使该复选框与复选框列表对齐。

【问题讨论】:

  • 你有这个的 html 吗?
  • 你考虑过使用 jQuery 吗?
  • html 只是带有它的项目的复选框列表。这是一个现有的系统,不确定我是否可以将 jQuery 添加到组合中。
  • 至少可以用谷歌之类的CDN添加jQuery试试,会有很大帮助

标签: javascript .net select checkboxlist selectall


【解决方案1】:

只需检查单击的框是否是全部选项。如果是,那么继续并更改其余的框。如果不是,则检查所有选项以查看它们是否都被选中,以便您可以更新“全部”复选框。


编辑

您可能想使用 onChange,而不是 onClick,可能会在给定复选框上的值更改之前调用 onClick。


代码未检查,语法问题请见谅。

<script language="javascript" type="text/javascript">
    function CheckBoxListSelect(cbControl) //, state)
    {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount= chkBoxList.getElementsByTagName("input");
        var clicked = this;

        alert(chkBoxCount[0].checked ? 'All is Checked' : 'All is not Checked');
        alert(clicked == chkBoxCount[0] ? 'You Clicked All' : 'You Didn't click All');

        var AllChecked = true; // Check the all box if all the options are now checked

        for(var i = 1;i < chkBoxCount.length; i++)
        {   
            if(clicked == chkBoxCount[0]) { // Was the 'All' checkbox clicked?
                chkBoxCount[i].checked = chkBoxCount[0].checked; // Set if so
            }
            AllChecked &= chkBoxCount[i].checked; // AllChecked is anded with the current box
        }

        chkBoxCount[0].checked = AllChecked;

        return false; 
    }
</script>

cblAffiliation.Attributes.Add("onchange", "javascript: CheckBoxListSelect ('" & cblAffiliation.ClientID & "');")

【讨论】:

    【解决方案2】:
     <asp:CheckBox ID="checkAll" runat="server" Text="Select/Unselect" onclick="CheckAll();" />
        <asp:CheckBoxList ID="chkTest" runat="server" onclick="ClearAll();">
            <asp:ListItem Text="Test 1" Value="0"></asp:ListItem>
            <asp:ListItem Text="Test 2" Value="1"></asp:ListItem>
            <asp:ListItem Text="Test 3" Value="2"></asp:ListItem>
                </asp:CheckBoxList>
    <script type="text/javascript">
    
    function CheckAll() {
        var intIndex = 0;
        var rowCount=document.getElementById('chkTest').getElementsByTagName("input").length;
        for (intIndex = 0; intIndex < rowCount; intIndex++) 
        {
            if (document.getElementById('checkAll').checked == true)
            {
                if (document.getElementById("chkTest" + "_" + intIndex)) 
                {
                    if (document.getElementById("chkTest" + "_" + intIndex).disabled != true)
                        document.getElementById("chkTest" + "_" + intIndex).checked = true;
                }
             }
             else
             {
                 if (document.getElementById("chkTest" + "_" + intIndex)) 
                    {
                        if (document.getElementById("chkTest" + "_" + intIndex).disabled != true)
                            document.getElementById("chkTest" + "_" + intIndex).checked = false;
                    }
             }
        }
     } 
    
    function ClearAll(){
        var intIndex = 0;
        var flag = 0;
        var rowCount=document.getElementById('chkTest').getElementsByTagName("input").length;
        for (intIndex = 0; intIndex < rowCount; intIndex++) 
        {
                if (document.getElementById("chkTest" + "_" + intIndex)) 
                {
                        if(document.getElementById("chkTest" + "_" + intIndex).checked == true)
                        {
                            flag=1;
                        }
                        else
                        {
                            flag=0;
                            break;
                        }
                }
        }
        if(flag==0)
             document.getElementById('checkAll').checked = false;
        else
             document.getElementById('checkAll').checked = true;
    
    }
    </script>
    

    【讨论】:

    • 请复制并粘贴您将得到答案的整个代码。意思是......所有彩色代码,下面是javascript函数将全部复制。
    【解决方案3】:

    我认为这段代码会对你有所帮助。

    <script type="text/javascript">
    function check(checkbox) 
    {
     var cbl = document.getElementById('<%=CheckBoxList2.ClientID %>').getElementsByTagName("input");
     for(i = 0; i < cbl.length;i++) cbl[i].checked = checkbox.checked;
    }
    </script>
    
     <asp:CheckBox ID="CheckBox1" runat="server" onclick="check(this)" />
     <asp:CheckBoxList ID="CheckBoxList2" runat="server">
        <asp:ListItem>C1</asp:ListItem>
        <asp:ListItem>C2</asp:ListItem>
      </asp:CheckBoxList>
    

    【讨论】:

      【解决方案4】:

       function checkparent(id) {
                  var parentid = id;
                  var Control = document.getElementById(parentid).getElementsByTagName("input");
                  if (parentid.indexOf("List") != -1) {
                      parentid = parentid.replace("List", "");
                  }
                  if (eval(Control).length > 0) {
                      if (eval(Control)) {
      
                          for (var i = 0; i < Control.length; i++) {
                              checkParent = false;
                              if (Control[i].checked == true) {
                                  checkChild = true;
                              }
                              else {
      
                                  checkChild = false;
                                  break;
                              }
                          }
                      }
                  }
                  if (checkParent == true && document.getElementById(parentid).checked == false) {
                      document.getElementById(parentid).checked = false;
                      checkParent = true;
                  }
                  else if (checkParent == true && document.getElementById(parentid).checked == true) {
                      document.getElementById(parentid).checked = true;
                      checkParent = true;
                  }
                  if (checkChild == true && checkParent == false) {
                      document.getElementById(parentid).checked = true;
                      checkParent = true;
                  }
                  else if (checkChild == false && checkParent == false) {
                      document.getElementById(parentid).checked = false;
                      checkParent = true;
                  }
      
              }
              function CheckDynamic(chkid) {
                  id = chkid.id;
                  var chk = $("#" + id + ":checked").length;
                  var child = id + "List";
                  if (chk != 0) {
                      $("[id*=" + child + "] input").attr("checked", "checked");
                  }
                  else {
                      $("[id*=" + child + "] input").removeAttr("checked");
                  }
                  checkparent(id);
              }
      

      【讨论】:

      • 这是动态功能,可用于所有复选框和复选框列表。使用上面的代码将复选框列表 ID 附加到列表 ex:(父 ID+列表) 声明我使用的变量。
      • 在checkbox点击和checkboxlist点击事件(CheckDynamic(this))中调用上述函数
      • var id = "";变种孩子=“”; var checkChild = null; var checkParent = true;
      • 全局声明上述变量
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多