js實(shí)現(xiàn)全選和全不選功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)全選和全不選的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>操作復(fù)選框</title></head><body><input type='checkbox' id='quan'> 全選<br><input type='checkbox' name='aihao'>游戲<br><input type='checkbox' name='aihao'>睡覺(jué)<br></body></html><script type='text/javascript'> window.onload=function () { var firstChecbox = document.getElementById('quan'); var aihao=document.getElementsByName('aihao'); //完成全選和全不選 //當(dāng)單擊全選時(shí)使下方的checkbox中的checked屬性為true firstChecbox.onclick=function () { //遍歷下方的checkbox //使每一個(gè)復(fù)選框的屬性中的checked和全選的屬性保持一致即可實(shí)現(xiàn)(不完善) for (let i = 0; i <aihao.length ; i++) { aihao[i].checked=firstChecbox.checked; } } //如果選中的數(shù)量和愛(ài)好的總數(shù)量一致的就把全選給選中,否則不全選 //為每一個(gè)aihao綁定單擊事件 var all=aihao.length; for (let i = 0; i < aihao.length; i++) { //綁定單擊事件 aihao[i].onclick=function () { //定義選中的數(shù)量 var checkedCount=0; for (let i = 0; i < aihao.length; i++) { //如果愛(ài)好選中就把選中的數(shù)量+1; if (aihao[i].checked){ checkedCount++; } //如果選中的數(shù)量和總數(shù)相當(dāng)就把全選給勾選 if (checkedCount==all){ firstChecbox.checked=true } else{ firstChecbox.checked=false; } } } } }</script>
更多關(guān)于復(fù)選框的文章請(qǐng)點(diǎn)擊專題:javascript復(fù)選框操作匯總、jquery復(fù)選框操作匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫(xiě)的方法2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))4. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)5. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法6. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )7. VMware中如何安裝Ubuntu8. docker容器調(diào)用yum報(bào)錯(cuò)的解決辦法9. springboot集成與使用Sentinel的方法10. django創(chuàng)建css文件夾的具體方法
