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'>睡覺<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ù)量和愛好的總數(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++) { //如果愛好選中就把選中的數(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ù)選框操作匯總
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. js實(shí)現(xiàn)碰撞檢測2. 一文帶你徹底理解Java序列化和反序列化3. JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁碼代碼實(shí)例4. 關(guān)于IDEA 2020.3 多窗口視圖丟失的問題5. PHP驗(yàn)證碼工具-Securimage6. 用Spring JMS使異步消息變得簡單7. 通過實(shí)例解析Python文件操作實(shí)現(xiàn)步驟8. Python3 json模塊之編碼解碼方法講解9. Python 制作查詢商品歷史價(jià)格的小工具10. Python 解析庫json及jsonpath pickle的實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備