1-JQuery - 复选框的操作

关于复选框的操作

获取所有的选中状态的复选框:

$("#chk1").find('input:checkbox').each(function() { //遍历所有复选框
 
    if ($(this).prop('checked') == true) {
 
        console.log($(this).val()); //打印当前选中的复选框的值
 
    }
 
});
 
function getCheckBoxVal(){ //jquery获取所有选中的复选框的值 
 
    var chk_value =[]; 
 
    $("#chk1").find('input[name="test"]:checked').each(function(){ //遍历,将所有选中的值放到数组中
 
        chk_value.push($(this).val()); 
 
    }); 
 
    alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value); 
 
} 

或者:

$("#sure").click(function () {
    var arr = new Array();
    $.each($(".p1"), function (index, item) {
        // console.log(index, item)
        if ($(item).get(0).checked) {
            arr.push($(item).val())
        }
    });
    if (arr.length == 0) {
        // 说明用户未选中用例,需要给提示
        // console.log(2222222, "未选中", arr);
        $("#errorMsg").html("请勾选至少一个用例!");

    } else {
        // 编写后续的操作
    }
});

that's all

see also:

jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等

posted @ 2020-05-07 14:24  听雨危楼  阅读(152)  评论(0编辑  收藏  举报