2012-03-06 134 views
0

在我的网页上,我有一个CheckBoxList和一个复选框。当我点击复选框时,CheckBoxList中的所有复选框应该被检查。我的CheckBoxList必须位于Bodycontent占位符下,因为这是网页布局的方式,并且我将脚本保留在相同的占位符中。单个复选框检查列表中的所有复选框点击

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
<script type="text/javascript"> 
      function select(ch) { 
       var allcheckboxes = document.getElementById('<%=CheckBoxList1.ClientID %>').getElementsByTagName("input"); 
       for (i = 0; i < allcheckboxes.length; i++) 
        allcheckboxes[i].checked = ch.checked; 
      } 
     </script> 

     <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
      RepeatDirection="Horizontal" RepeatLayout="Flow"> 
      <asp:ListItem>Item A</asp:ListItem> 
      <asp:ListItem>Item B</asp:ListItem> 
      <asp:ListItem>Item C</asp:ListItem> 
     </asp:CheckBoxList> 

      <asp:CheckBox ID="allCheck" onclick="select(this)" runat="server" Text="Select all" /> 
       <br /> 
</asp:Content> 

以上不做任何事情。在复选框上点击没有任何反应!我一直被困在这个小问题上,而且不能这样做。任何建议有什么不对?

+0

没有尝试调试JavaScript与一些JS调试器,例如。 Firebug? – 2012-03-06 11:37:57

回答

2

将您的函数的名称更改为其他名称;它会工作

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
<script type="text/javascript"> 
      function select1(ch) { 
       var allcheckboxes = document.getElementById('<%=CheckBoxList1.ClientID %>').getElementsByTagName("input"); 
       for (i = 0; i < allcheckboxes.length; i++) 
        allcheckboxes[i].checked = ch.checked; 
      } 
     </script> 

     <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
      RepeatDirection="Horizontal" RepeatLayout="Flow"> 
      <asp:ListItem>Item A</asp:ListItem> 
      <asp:ListItem>Item B</asp:ListItem> 
      <asp:ListItem>Item C</asp:ListItem> 
     </asp:CheckBoxList> 

      <asp:CheckBox ID="allCheck" onclick="select1(this)" runat="server" Text="Select all" /> 
       <br /> 
</asp:Content> 
0

尝试这样的..

function UnCheckAll(isCheck) { 
      var theForm = document.forms['yourFormName']; 
      if (!theForm) { 
       theForm = document.form1; 
      } 
      var length = theForm.elements.length; 
      for (var i = 0; i < length; i++) { 
       if (theForm.elements[i].type == "checkbox") { 
        if (theForm.elements[i].id != "allCheck") { 
         if (theForm.elements[i].disabled == false) { 
          theForm.elements[i].checked = isCheck.checked; 
         } 
        } 
       } 
      } 
     } 
相关问题