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ù)選框操作匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明2. CSS hack用法案例詳解3. ASP 處理JSON數(shù)據(jù)的實(shí)現(xiàn)代碼4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問(wèn)題7. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法
