2012-08-09 21 views
0

我在我的網頁上有三個不同的複選框列表。我想要一個複選框,顯示「選擇全部」,當該複選框被選中時,所有三個複選框列表複選框都會被選中。我正在看這個例子,通過點擊選擇按鈕來檢查一個複選框列表,但我希望所有三個複選框列表複選框在javascript中檢查。下面是我的代碼點擊一個按鈕檢查多個checkboxlist

<asp:CheckBox ID="chkCheckAll" runat="server" Text="Check/Uncheck All" 
             Style="font-weight: 700"  CausesValidation="false" oncheckedchanged="chkCheckAll_CheckedChanged" AutoPostBack="true" 
              /> 

    <asp:CheckBoxList ID="chkList_MetricsSeverity" runat="server" RepeatDirection="Horizontal" 
               RepeatColumns="3" Width="1060px"> 
              </asp:CheckBoxList> 

<asp:CheckBoxList ID="chkList_MetricsAvgMedian" runat="server" RepeatDirection="Horizontal" 
               RepeatColumns="3" Width="1060px"> 
              </asp:CheckBoxList> 

<asp:CheckBoxList ID="chkList_Counts" runat="server" RepeatDirection="Horizontal" 
               RepeatColumns="3" Width="1060px"> 
              </asp:CheckBoxList> 

任何幫助將不勝感激

+0

你是否使用jquery? – 2012-08-09 18:16:26

回答

0

這是需要完成的工作。下面是代碼

function CheckAll() { 


     var chkbx = document.getElementById('<%=chkCheckAll.ClientID %>'); 

     var chkbxList1 = document.getElementById('<%=chkList_MetricsSeverity.ClientID %>'); 

     var chkbxList2 = document.getElementById('<%=chkList_MetricsAvgMedian.ClientID %>'); 

     var chkbxList3 = document.getElementById('<%=chkList_Counts.ClientID %>'); 
     if (chkbx.checked == true) { 
      var chkbxListCount = chkbxList1.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = true; 
      } 
      var chkbxListCount = chkbxList2.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = true; 
      } 
      var chkbxListCount = chkbxList3.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = true; 
      } 
     } 
     else { 
      var chkbxListCount = chkbxList1.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = false; 
      } 
      var chkbxListCount = chkbxList2.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = false; 
      } 
      var chkbxListCount = chkbxList3.getElementsByTagName('input'); 
      for (var i = 0; i < chkbxListCount.length; i++) { 
       chkbxListCount[i].checked = false; 
      } 
     } 




    } 
2

看一看here ...你可以找到實現上述兩個asp.net和JavaScript的方式的東西的方式......

+0

我已經看到那篇文章,但它只針對一個複選框列表。我需要在javascript函數中傳遞多個checkboxlist的id。我不知道該怎麼做。 – 2012-08-09 20:04:20