如何获得复选框内表行单元格

How to get checkbox inside table row cell

本文关键字:单元格 何获得 复选框      更新时间:2023-09-26

我是一个javascript新手。

我试图提醒表视图行单元格内的复选框的id。

我试着:

var checkBox = grid.rows[i].cells[0].firstChild;
alert(checkBox.id);

但是警告框显示"undefined"

我找到了答案,而不是使用

grid.rows[i].cells[0].firstChild 

I tried

grid.rows[i].cells[0].children[0] 

,它工作得很好。

谢谢

这对我有用:

function check() {
    let table = $('#tableId').DataTable();
    let rows = table.rows({selected: true}).nodes().to$();
    var checkBox = rows[0].cells[0].children[0];
    alert(checkBox.id)
}

你可以使用jquery,这样做:'

<html>
<head>
<script src="jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
//alert();
$(".chk").bind("click",function(){
          alert($(this).attr("id"));
          //alert();
    });
});
    </script>
    </head>
    <body>
            <form action="">
        <input type="checkbox" class="chk" id="one" name="vehicle" value="Bike">I have a bike<br>
        <input type="checkbox" class="chk" id="two" name="vehicle" value="Car">I have a car 
</form>
    </body>
    </html>

"

给复选框指定id

    <input type='checkbox' id='chkbox1' name='ex1_b'>
    var checkbox = $('#chkbox1');    
    alert(checkbox.attr('id'))
   var table = document.getElementById("tblRestaurantTiming");
        //Loop through Table Rows.
        for (var i = 0; i < table.rows.length - 1; i++) {
            //Reference the Table Row.
            var row = table.rows[i];   
            var lastorder = row.cells[6].firstChild;
            var check = lastorder.checked;
           }