如何获取单元格中的所有复选框值

时间:2018-05-15 04:40:40

标签: javascript jquery datatables

我有jQuery数据表,其中包含一些用于授予权限的复选框。  我需要将表值获取到数组。请告诉我如何获取单元格内的复选框状态,而不仅仅是已检查的复选框状态。谢谢。

我的表

 <table id="jqueryTable" name="tt" class="table table-striped table-bordered" cellspacing="0">
    <thead>
        <tr>
            <th name="id">
                ID
            </th>
            <th name="name">
                PRIV_Name_Str
            </th>
            <th name="create">
                Create
            </th>
            <th>
                Edit
            </th>
            <th>
                View
            </th>
        </tr>
    </thead>
<table>

我的数据表查询

function LoadProduct(element) {
$.ajax({
    url: '/ADM_MAS_Privilege/GetFormData',
    data: { YourValue: $('#productCategory').val() },
    method: 'post',
    dataType: 'json',
    success: function (data) {
        var table =   $('#jqueryTable').dataTable({
            paging: true,
            sort: true,
            searching: true,
            scrollY: 200,
            data: data,
            bDestroy: true,

            "columnDefs":
            [{
                "targets": [2, 3, 4],
                "render": function (data, type, row, meta) {
                    console.log("XX " + meta.row + " " + meta.col);
                    return type === 'display' ?
                        '<input type="checkbox" id="p" class="chk" name="group' + meta.row + '" /> ' + data :
                        data;
            columns: [{ "data": "ID", "ID": "ID", "autoWidth": true },
            {
                "data": "PRIV_Name_Str", "PRIV_Name_Str": "PRIV_Name_Str", "autoWidth": true
            },
            {
                "data": "Create", "Create": "Create", "autoWidth": true
            },
            { "data": "Edit", "Edit": "Edit", "autoWidth": true },
            {
                "data": "View"                 

            }

            ]
        });
    }
});

};

我的jQuery函数读取数据表

$('#upload').click(function () {
var table = document.getElementById("jqueryTable");
var tableArr = [];
for (var i = 1; i < table.rows.length; i++) {
    tableArr.push({

        ID: table.rows[i].cells[0].innerHTML,
        PRIV_Name_Str: table.rows[i].cells[1].innerHTML,

        Create: table.rows[i].cells[2].innerHTML,
        Edit: table.rows[i].cells[3].innerHTML,

        View: table.rows[i].cells[4].innerHTML

    });
}

});

我试过table.rows [i] .cells [2] .innerHTML.getElementById(&#34; p&#34;)。检查均匀并且它不起作用。

2 个答案:

答案 0 :(得分:1)

因为你正在使用jQuery:

document.getElementById("jqueryTable").each(function (index, element) {
  tableArr.push({
    ID: element.cells[0].innerHTML,
    PRIV_Name_Str: element.cells[1].innerHTML,

    Create: element.cells[2].innerHTML,
    Edit: element.cells[3].innerHTML,

    View: element.cells[4].innerHTML
  })
})

答案 1 :(得分:0)

您可以在每个单元格中找到元素和checked属性:

for (var i = 1; i < table.rows.length; i++) {
    var cells = table.rows[i].cells;
    tableArr.push({
        ID: cells[0].innerHTML,
        PRIV_Name_Str: cells[1].innerHTML,
        Create: cells[2].querySelectorAll('input')[0].checked,
        Edit: cells[3].querySelectorAll('input')[0].checked,
        View: cells[4].querySelectorAll('input')[0].checked 
    });
}