checkbox

復(fù)習(xí)一下原生的checkbox #35

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>


</head>
<body>
    <input type="checkbox" name="c0" id="all" value="全選" onclick="change()">全選
<hr>
<table>
<tr>
<td><input type="checkbox" name="c1"">籃球</td>
</tr>
<tr>
<td><input type="checkbox" name="c1">足球</td>
</tr>
<tr>
<td><input type="checkbox" name="c1">羽毛球</td>
</tr>
<tr>
<td><input type="checkbox" name="c1">兵乓球</td>
</tr>
</table>
<button onclick="submit()">提交</button>
</body>
</html>
<script type="text/javascript">
    //獲取checkbox按鈕組
    var allpro = document.getElementsByName("c1");
    //獲取全選按鈕
    var all = document.getElementById("all");

    let arr = [];
    //全選方法
    function change() {
        //全選按鈕被選中時(shí),遍歷所有按鈕
        if (all.checked) {
            for (var i = 0; i < allpro.length; i++) {
                if (allpro[i].type=="checkbox") {
                    allpro[i].checked=true;
                }
            }
        // 全選按鈕未被選中時(shí)
        }else{
            for (var i = 0; i < allpro.length; i++) {
                if (allpro[i].type=="checkbox") {
                    allpro[i].checked=false;
                }
            }
        }
        getCheckhtml();
    }

    // 給每一個(gè)子集check添加點(diǎn)擊事件
    function childCheck () {
        for (let i = 0; i < allpro.length ; i ++) {
            addEventHandler(allpro[i], 'click', checkall);
        }
    }
    function checkall () {
        let count = 0;
        for (let i = 0; i < allpro.length; i ++ ) {
            if (allpro[i].checked == true) {
                count ++;
            }
        }
        if (count == allpro.length) {
            all.checked = true;
        } else {
            all.checked = false;
        }
        getCheckhtml();
    }

    // 獲取選中check的內(nèi)容
    function getCheckhtml () {
        arr = [];
        for (let i = 0; i < allpro.length; i ++ ) {
            if (allpro[i].type=="checkbox" && allpro[i].checked == true) {
                arr.push(allpro[i].parentNode.innerText);
            }
        }
    }

    // 提交
    function submit () {
        alert(arr);
    }


    /* 
    * addEventListener:監(jiān)聽Dom元素的事件 
    *   
    *  target:監(jiān)聽對象 
    *  type:監(jiān)聽函數(shù)類型,如click,mouseover 
    *  func:監(jiān)聽函數(shù) 
    */  
    function addEventHandler(target,type,func){  
        if(target.addEventListener){  
            //監(jiān)聽IE9,谷歌和火狐  
            target.addEventListener(type, func, false);  
        }else if(target.attachEvent){  
            target.attachEvent("on" + type, func);  
        }else{  
            target["on" + type] = func;  
        }   
    }  
    /* 
    * removeEventHandler:移除Dom元素的事件 
    *   
    *  target:監(jiān)聽對象 
    *  type:監(jiān)聽函數(shù)類型,如click,mouseover 
    *  func:監(jiān)聽函數(shù) 
    */  
    function removeEventHandler(target, type, func) {  
        if (target.removeEventListener){  
            //監(jiān)聽IE9,谷歌和火狐  
            target.removeEventListener(type, func, false);  
        } else if (target.detachEvent){  
            target.detachEvent("on" + type, func);  
        }else {  
            delete target["on" + type];  
        }  
    }  

    childCheck();
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容