table获取checkbox是否选中的几种方法

function test() {

    $(".table tbody tr").find("td:first input:checkbox").each(function () {
        var ischecked = $(this).prop("checked");
        alert(ischecked);

    });

    $(".table input:checkbox").each(function () {
        if ($(this).attr("checked") == true) {
            alert($("table input:checkbox").index(this))
        }
    });

    $(".table tr td:eq(0)").each(function () {

        $(this).find("input[type=checkbox]").prop("checked", "checked");

    });


    var data = [];

    $(".table tbody tr").find(":checkbox:checked").each(function () {
        var val = $(this).parent().next().text();
        data.push(val);
        alert(val);
    });
    alert(data);
}

function ck() {

    ////正确 1
    var tbodyObj = document.getElementById('tbodydata');
    $("table :checkbox").each(function (key, value)
    {
        if ($(value).prop('checked'))
        {
            alert(tbodyObj.rows[key].cells[1].innerHTML);
            alert(tbodyObj.rows[key].cells[2].innerHTML);
        }
    })

    //获取选中的数据组  案例           
    var array = $(".table tbody input[type=checkbox]:checked").map(function () {
        return { "cell2": $.trim($(this).closest("tr").find("td:eq(2)").text()), "cell4": $.trim($(this).closest("tr").find("td:eq(4)").text()) };
    }).get();
    $.each(array, function (i, d) { alert(d.cell2 + "|" + d.cell4); })

    //$.each(array, function (i, d) { alert(d.cell2 + "|" + d.cell4); })

    //获取选中的数据组  测试组装值是否正确              
    var array = $(".table tbody input[type=checkbox]:checked").map(function () {
        alert($.trim($(this).closest("tr").find("td:eq(1)").text()));
        alert($.trim($(this).closest("tr").find("td:eq(2)").text()));
        alert($.trim($(this).closest("tr").find("td:eq(3)").text()));
        alert($.trim($(this).closest("tr").find("td:eq(2)").text()));
        //return { "cell2": $.trim($(this).closest("tr").find("td:eq(2)").text()), "cell4": $.trim($(this).closest("tr").find("td:eq(4)").text()) };
    }).get();

    ///把取出来的的值放入数组
    var array = $(".table tbody input[type=checkbox]:checked").map(function () {
        return {
            "code": $.trim($(this).closest("tr").find("td:eq(2)").text()),
            "name": $.trim($(this).closest("tr").find("td:eq(4)").text())
        };
    }).get();
    $.each(array, function (i, d) { alert(d.code + "|" + d.name); })
}

function test2() {
    var selectedData = [];
    $(":checkbox:checked").each(function () {
        var tablerow = $(this).parent("tr");
        var code = tablerow.find("[name='p_code']").val();
        var name = tablerow.find("[name='p_name']").val();
        var price = tablerow.find("[name='p_price']").val();
        selectedData.push({ Code: code, Name: name, Price: price });
    });

    var selectedData2 = [];
    $(":checkbox:checked").each(function () {
        var tablerow = $(this).parent("tr");
        var code = tablerow.find("[name='p_code']").val();
        var name = tablerow.find("[name='p_name']").val();
        var price = tablerow.find("[name='p_price']").val();
        selectedData2.push({ Code: code, Name: name, Price: price });
    });
    var selectedData3 = [];
    $(":checkbox:checked", ".table").each(function () {
            var tablerow = $(this).parent("tr");
            var code = tablerow.find("[name='p_code']").val();
            var name = tablerow.find("[name='p_name']").val();
            var price = tablerow.find("[name='p_price']").val();
            selectedData3.push({ Code: code, Name: name, Price: price });
        });

}

原文地址:https://www.cnblogs.com/jpfss/p/7656356.html