js操作CheckBoxList實現(xiàn)全選/反選(在客服端完成)
更新時間:2013年02月02日 17:14:16 作者:
對于CheckBoxList控件來說,一方面要實現(xiàn)大量數(shù)據(jù)在服務器端的綁定工作,另一方面往往要求實現(xiàn)全選、反選等功能,接下來將介紹js操作CheckBoxList實現(xiàn)全選/反選,感興趣的朋友可以了解下,或許對你有所幫助
對于CheckBoxList控件來說,一方面要實現(xiàn)大量數(shù)據(jù)在服務器端的綁定工作,另一方面往往要求實現(xiàn)全選、反選等功能。雖然可以在服務器端完成這方面的工作,但這樣一個簡單的工作似乎更應該在客戶端完成。
具體方法:
在頁面中放入一個CheckBoxList控件,并添加幾項,用來分析其產生的HTML代碼,這樣在使用js進行
動態(tài)控制時,將會非常清晰其測試代碼如下所示:
<asp:CheckBoxListID="CheckBoxList1"runat="server"CellPadding="3"CellSpacing="3"
RepeatColumns="3">
<asp:ListItem>1232</asp:ListItem>
<asp:ListItem>254</asp:ListItem>
<asp:ListItemValue="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代碼。
<tableid="CheckBoxList1"cellspacing="3"cellpadding="3"border="0">
<tr>
<td><inputid="CheckBoxList1_0"type="checkbox"name="CheckBoxList1$0"/><labelfor="CheckBoxList1_0">1232</label>
</td>
<td><inputid="CheckBoxList1_4"type="checkbox"name="CheckBoxList1$4"/><labelfor="CheckBoxList1_4">654</label>
</td>
.......
</table>
在這里,節(jié)選了部分代碼,其中藍色部分是我們關心的。在HTML中CheckBoxList生成了
許多input(type為checkbox),并且其ID為“CheckBoxList1_i”(i為數(shù)字)。這樣我們只
需要知道一共有幾項就可以輕松的實現(xiàn)js對它的控制。
這些input都包含在一個id為CheckBoxList1的table中,因此可以通過:
document.getElementById("CheckBoxList1").getElementsByTagName("input").length
這一方法獲取CheckBoxList一共有多少項,剩下的工作其實就很簡單了,通過js更改每一個
checkbox的狀態(tài)即可。先添加三個button,用來實現(xiàn)全選、反選及清除控制,如下所示:
<inputtype="button"onclick="checkAll()"value="checkAll"/>
<inputtype="button"onclick="ReverseAll()"value="ReverseAll"id="Button1"/>
<inputtype="button"onclick="deleteAll()"value="deleteAll"/>
添加全選、反選及清除函數(shù)如下:
functioncheckAll(){
//alert(document.getElementById("CheckBoxList1").getElementsByTagName("input").length);
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
document.getElementById("CheckBoxList1_"+i).checked=true;
}
}
functiondeleteAll(){
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
document.getElementById("CheckBoxList1_"+i).checked=false;
}
}
functionReverseAll(){
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
varobjCheck=document.getElementById("CheckBoxList1_"+i);
if(objCheck.checked)
objCheck.checked=false;
else
objCheck.checked=true;
}
}
OK,現(xiàn)在通過IE測試,綁定工作可以在后臺,全選等輔助功能可以自由發(fā)揮了!
具體方法:
在頁面中放入一個CheckBoxList控件,并添加幾項,用來分析其產生的HTML代碼,這樣在使用js進行
動態(tài)控制時,將會非常清晰其測試代碼如下所示:
復制代碼 代碼如下:
<asp:CheckBoxListID="CheckBoxList1"runat="server"CellPadding="3"CellSpacing="3"
RepeatColumns="3">
<asp:ListItem>1232</asp:ListItem>
<asp:ListItem>254</asp:ListItem>
<asp:ListItemValue="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代碼。
復制代碼 代碼如下:
<tableid="CheckBoxList1"cellspacing="3"cellpadding="3"border="0">
<tr>
<td><inputid="CheckBoxList1_0"type="checkbox"name="CheckBoxList1$0"/><labelfor="CheckBoxList1_0">1232</label>
</td>
<td><inputid="CheckBoxList1_4"type="checkbox"name="CheckBoxList1$4"/><labelfor="CheckBoxList1_4">654</label>
</td>
.......
</table>
在這里,節(jié)選了部分代碼,其中藍色部分是我們關心的。在HTML中CheckBoxList生成了
許多input(type為checkbox),并且其ID為“CheckBoxList1_i”(i為數(shù)字)。這樣我們只
需要知道一共有幾項就可以輕松的實現(xiàn)js對它的控制。
這些input都包含在一個id為CheckBoxList1的table中,因此可以通過:
復制代碼 代碼如下:
document.getElementById("CheckBoxList1").getElementsByTagName("input").length
這一方法獲取CheckBoxList一共有多少項,剩下的工作其實就很簡單了,通過js更改每一個
checkbox的狀態(tài)即可。先添加三個button,用來實現(xiàn)全選、反選及清除控制,如下所示:
復制代碼 代碼如下:
<inputtype="button"onclick="checkAll()"value="checkAll"/>
<inputtype="button"onclick="ReverseAll()"value="ReverseAll"id="Button1"/>
<inputtype="button"onclick="deleteAll()"value="deleteAll"/>
添加全選、反選及清除函數(shù)如下:
復制代碼 代碼如下:
functioncheckAll(){
//alert(document.getElementById("CheckBoxList1").getElementsByTagName("input").length);
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
document.getElementById("CheckBoxList1_"+i).checked=true;
}
}
functiondeleteAll(){
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
document.getElementById("CheckBoxList1_"+i).checked=false;
}
}
functionReverseAll(){
for(vari=0;i<document.getElementById("CheckBoxList1").getElementsByTagName("input").length;i++)
{
varobjCheck=document.getElementById("CheckBoxList1_"+i);
if(objCheck.checked)
objCheck.checked=false;
else
objCheck.checked=true;
}
}
OK,現(xiàn)在通過IE測試,綁定工作可以在后臺,全選等輔助功能可以自由發(fā)揮了!
相關文章
JavaScript動態(tài)創(chuàng)建form表單并提交的實現(xiàn)方法
這篇文章主要介紹了JavaScript動態(tài)創(chuàng)建form表單并提交的實現(xiàn)方法,涉及JavaScript動態(tài)創(chuàng)建頁面元素及模擬表單提交的技巧,需要的朋友可以參考下2015-12-12js實現(xiàn)文本框只允許輸入數(shù)字并限制數(shù)字大小的方法
這篇文章主要介紹了js實現(xiàn)文本框只允許輸入數(shù)字并限制數(shù)字大小的方法,涉及javascript對字符串正則判斷及數(shù)值大小判斷的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08JS開發(fā)中百度地圖+城市聯(lián)動實現(xiàn)實時觸發(fā)查詢地址功能
這篇文章主要介紹了JS開發(fā)中百度地圖+城市聯(lián)動實現(xiàn)實時觸發(fā)查詢地址功能,需要的朋友可以參考下2017-04-04JavaScript 函數(shù)用法詳解【函數(shù)定義、參數(shù)、綁定、作用域、閉包等】
這篇文章主要介紹了JavaScript 函數(shù)用法,結合實例形式分析了JavaScript函數(shù)定義、參數(shù)、綁定、作用域、閉包、回調函數(shù)、柯理化函數(shù)等相關概念、原理與操作注意事項,需要的朋友可以參考下2020-05-05jscript之Read an Excel Spreadsheet
jscript之Read an Excel Spreadsheet...2007-06-06JavaScript Canvas實現(xiàn)井字棋游戲
這篇文章主要為大家詳細介紹了JavaScript Canvas實現(xiàn)井字棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08