js操作CheckBoxList實(shí)現(xiàn)全選/反選(在客服端完成)
更新時(shí)間:2013年02月02日 17:14:16 作者:
對于CheckBoxList控件來說,一方面要實(shí)現(xiàn)大量數(shù)據(jù)在服務(wù)器端的綁定工作,另一方面往往要求實(shí)現(xiàn)全選、反選等功能,接下來將介紹js操作CheckBoxList實(shí)現(xiàn)全選/反選,感興趣的朋友可以了解下,或許對你有所幫助
對于CheckBoxList控件來說,一方面要實(shí)現(xiàn)大量數(shù)據(jù)在服務(wù)器端的綁定工作,另一方面往往要求實(shí)現(xiàn)全選、反選等功能。雖然可以在服務(wù)器端完成這方面的工作,但這樣一個(gè)簡單的工作似乎更應(yīng)該在客戶端完成。
具體方法:
在頁面中放入一個(gè)CheckBoxList控件,并添加幾項(xiàng),用來分析其產(chǎn)生的HTML代碼,這樣在使用js進(jìn)行
動(dòng)態(tài)控制時(shí),將會(huì)非常清晰其測試代碼如下所示:
<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進(jìn)行分析:以下是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é)選了部分代碼,其中藍(lán)色部分是我們關(guān)心的。在HTML中CheckBoxList生成了
許多input(type為checkbox),并且其ID為“CheckBoxList1_i”(i為數(shù)字)。這樣我們只
需要知道一共有幾項(xiàng)就可以輕松的實(shí)現(xiàn)js對它的控制。
這些input都包含在一個(gè)id為CheckBoxList1的table中,因此可以通過:
document.getElementById("CheckBoxList1").getElementsByTagName("input").length
這一方法獲取CheckBoxList一共有多少項(xiàng),剩下的工作其實(shí)就很簡單了,通過js更改每一個(gè)
checkbox的狀態(tài)即可。先添加三個(gè)button,用來實(shí)現(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測試,綁定工作可以在后臺(tái),全選等輔助功能可以自由發(fā)揮了!
具體方法:
在頁面中放入一個(gè)CheckBoxList控件,并添加幾項(xiàng),用來分析其產(chǎn)生的HTML代碼,這樣在使用js進(jìn)行
動(dòng)態(tài)控制時(shí),將會(huì)非常清晰其測試代碼如下所示:
復(fù)制代碼 代碼如下:
<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進(jìn)行分析:以下是DropDownList控件生成的HTML代碼。
復(fù)制代碼 代碼如下:
<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é)選了部分代碼,其中藍(lán)色部分是我們關(guān)心的。在HTML中CheckBoxList生成了
許多input(type為checkbox),并且其ID為“CheckBoxList1_i”(i為數(shù)字)。這樣我們只
需要知道一共有幾項(xiàng)就可以輕松的實(shí)現(xiàn)js對它的控制。
這些input都包含在一個(gè)id為CheckBoxList1的table中,因此可以通過:
復(fù)制代碼 代碼如下:
document.getElementById("CheckBoxList1").getElementsByTagName("input").length
這一方法獲取CheckBoxList一共有多少項(xiàng),剩下的工作其實(shí)就很簡單了,通過js更改每一個(gè)
checkbox的狀態(tài)即可。先添加三個(gè)button,用來實(shí)現(xiàn)全選、反選及清除控制,如下所示:
復(fù)制代碼 代碼如下:
<inputtype="button"onclick="checkAll()"value="checkAll"/>
<inputtype="button"onclick="ReverseAll()"value="ReverseAll"id="Button1"/>
<inputtype="button"onclick="deleteAll()"value="deleteAll"/>
添加全選、反選及清除函數(shù)如下:
復(fù)制代碼 代碼如下:
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測試,綁定工作可以在后臺(tái),全選等輔助功能可以自由發(fā)揮了!
您可能感興趣的文章:
- js實(shí)現(xiàn)checkbox全選和反選示例
- js實(shí)現(xiàn)checkbox全選、不選與反選的方法
- AngularJS實(shí)現(xiàn)全選反選功能
- javaScript checkbox 全選/反選及批量刪除
- 利用Vue.js實(shí)現(xiàn)checkbox的全選反選效果
- 兼容ie和firefox版本的js反選 全選 多選框
- javascript實(shí)現(xiàn)簡單的全選和反選功能
- js html css實(shí)現(xiàn)復(fù)選框全選與反選
- javaScript實(shí)現(xiàn)復(fù)選框全選反選事件詳解
- JavaScript實(shí)現(xiàn)全選或反選功能
相關(guān)文章
js(jquery)實(shí)現(xiàn)無刷新跳轉(zhuǎn)404頁面不存在效果
有時(shí)候我們希望臨時(shí)讓某個(gè)分類或者多個(gè)文章不能正常訪問,手動(dòng)給html文件改名?或者改后臺(tái)改程序?太麻煩了。用本文的js代碼很容易實(shí)現(xiàn),而且使用得當(dāng)很隱蔽。這篇文章主要介紹了js(jquery)實(shí)現(xiàn)無刷新跳轉(zhuǎn)404頁面不存在效果,需要的朋友可以參考下2023-04-04
基于javascript實(shí)現(xiàn)的購物商城商品倒計(jì)時(shí)實(shí)例
本文主要介紹了基于javascript實(shí)現(xiàn)的購物商城商品倒計(jì)時(shí)實(shí)例,代碼詳細(xì),可直接復(fù)制試試看效果。需要的朋友可以參考借鑒2016-12-12
微信小程序canvas實(shí)現(xiàn)環(huán)形漸變
這篇文章主要為大家詳細(xì)介紹了微信小程序canvas實(shí)現(xiàn)環(huán)形漸變,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
解決bootstrap中下拉菜單點(diǎn)擊后不關(guān)閉的問題
今天小編就為大家分享一篇解決bootstrap中下拉菜單點(diǎn)擊后不關(guān)閉的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Canvas?drawImage方法實(shí)現(xiàn)圖片壓縮詳解
這篇文章主要為大家介紹了Canvas?drawImage方法實(shí)現(xiàn)圖片壓縮詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
淺談layui 綁定form submit提交表單的注意事項(xiàng)
今天小編就為大家分享一篇淺談layui 綁定form submit提交表單的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
js數(shù)組的基本用法及數(shù)組根據(jù)下標(biāo)(數(shù)值或字符)移除元素
js數(shù)組的用法包括創(chuàng)建、取值賦值、添加以及根據(jù)下標(biāo)(數(shù)值或字符)移除元素等等,在本文將為大家詳細(xì)介紹下,感興趣的朋友可以參考下2013-10-10

