javascript控制伺服器控制項-js操作CheckBoxList實現全選、反選

來源:互聯網
上載者:User

轉自:http://hi.baidu.com/rayshow/blog/item/db300a60aff610da8db10d01.html

javascript   伺服器控制項   CheckBoxList   全選 反選

       對於CheckBoxList控制項來說,一方面要實現大量資料在伺服器端的綁定工作,另一方面往往要求實現全

選、反選等功能。雖然可以在伺服器端完成這方面的工作,但這樣一個簡單的工作似乎更應該在用戶端完

成。

       具體方法:

       在頁面中放入一個CheckBoxList控制項,並添加幾項,用來分析其產生的HTML代碼,這樣在使用js進行

動態控制時,將會非常清晰其測試代碼如下所示:

       <asp:CheckBoxList ID="CheckBoxList1" runat="server" CellPadding="3" CellSpacing="3"

            RepeatColumns="3">

            <asp:ListItem>1232</asp:ListItem>

            <asp:ListItem>254</asp:ListItem>

            <asp:ListItem Value="5643">5643</asp:ListItem>

            <asp:ListItem>789</asp:ListItem>

            <asp:ListItem>654</asp:ListItem>

            <asp:ListItem>564</asp:ListItem>

            <asp:ListItem>8564</asp:ListItem>

            <asp:ListItem>8564</asp:ListItem>

            <asp:ListItem>5452</asp:ListItem>

            <asp:ListItem>5641</asp:ListItem>

        </asp:CheckBoxList>

    在瀏覽器中查看,並對Html進行分析:以下是DropDownList控制項產生的HTML代碼。

<table id="CheckBoxList1" cellspacing="3" cellpadding="3" border="0">
         <tr>
            <td><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" /><label for="CheckBoxList1_0">1232</label>

           </td>

           <td><input id="CheckBoxList1_4" type="checkbox" name="CheckBoxList1$4" /><label for="CheckBoxList1_4">654</label>

          </td>

.......

</table>

     在這裡,節選了部分代碼,其中藍色部分是我們關心的。在HTML中CheckBoxList產生了

許多input(type為checkbox),並且其ID為“CheckBoxList1_i”(i為數字)。這樣我們只

需要知道一共有幾項就可以輕鬆的實現js對它的控制。

     這些input都包含在一個id為CheckBoxList1的table中,因此可以通過:

document.getElementById("CheckBoxList1").getElementsByTagName("input").length

這一方法擷取CheckBoxList一共有多少項,剩下的工作其實就很簡單了,通過js更改每一個

checkbox的狀態即可。先添加三個button,用來實現全選、反選及清除控制,如下所示:

          <inputtype="button" onclick="checkAll()" value="check All" />

        <inputtype="button"onclick="ReverseAll()"value="Reverse All"id="Button1"/>   

        <input type="button" onclick="deleteAll()" value="delete All" />

     添加全選、反選及清除函數如下:

     function checkAll(){

//            alert(document.getElementById("CheckBoxList1").getElementsByTagName("input").length);

            for(var i=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)

            {

                document.getElementById("CheckBoxList1_"+i).checked=true;

            }            

        }

        function deleteAll(){

            for(var i=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)

             {

                document.getElementById("CheckBoxList1_"+i).checked = false;

            }

        }

        function ReverseAll(){

            for(var i=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)

             {

                var objCheck = document.getElementById("CheckBoxList1_"+i);

                if(objCheck.checked)

                    objCheck.checked = false;

                else

                    objCheck.checked = true;

            }

        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.