JS checkbox 全选 全不选


/*
    JS checkbox 全选 全不选
    
    Html中checkbox: <input type="checkbox" name="cbx" value="<%= Default by yourself %>"/>
    以下方法各有优劣,使用时根据情况而定.
*/

//全选(方法一:each 循环)
function checkAll() {
    $.each($("input[name=cbx]"), function(i) {
        if ($(this).attr("checked") == false) {
            $(this).attr("checked", "true");
        }
    });
}

//全选(方法二:根据名称)
function checkAll() {
    var code_Values = document.all['cbx'];
    if(code_Values.length){
        for(var i=0;i<code_Values.length;i++){
            code_Values[i].checked = true;
        }
    }else{
        code_Values.checked = true;
    }
}

//全选(方法三:根据Tag 和 Type [input中的type])
function checkAll() {
    var code_Values = document.getElementsByTagName("input");
    if (code_Values.length) {
        for (i = 0; i < code_Values.length; i++) {
            if (code_Values[i].type == "checkbox") {
                code_Values[i].checked = true;
            }
        }
    } else {
        if (code_Values.type == "checkbox") {
            code_Values.checked = true;
        }
    }
}

//全不选(方法一:each 循环)
function uncheckAll() {
    $.each($("input[name=cbx]"), function(i) {
        if ($(this).attr("checked") == true) {
            $(this).attr("checked", "false");
        }
    });
}

//全不选(方法二:根据名称)
function uncheckAll()
{
    var code_Values = document.all['cbx'];
    if(code_Values.length){
    for(var i=0;i<code_Values.length;i++){
        code_Values[i].checked = false;
    }
    }else{
        code_Values.checked = false;
    }
}

//全不选(方法三:根据Tag 和 Type [input中的type])
function uncheckAll() {
    var code_Values = document.getElementsByTagName("input");
    if (code_Values.length) {
        for (i = 0; i < code_Values.length; i++) {
            if (code_Values[i].type == "checkbox") {
                code_Values[i].checked = false;
            }
        }
    } else {
        if (code_Values.type == "checkbox") {
            code_Values.checked = false;
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM