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

本文介绍了一种使用jQuery操作表格的方法,包括如何选取表格中的特定元素、获取被选中复选框的相关信息,并将这些信息组织成不同的数据结构,如数组或对象等。通过这些实用的代码片段,开发者可以更高效地进行前端表格数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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() {
    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 });
        });

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值