简体   繁体   中英

How to get checkbox inside table row cell

I'm very noob in javascript.

I'm trying to alert the id of a checkbox inside table view row cell.

I tried:

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

but the alert box shows "undefined".

I found the answer, instead of using

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

I tried

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

and it works perfectly.

Thanks

You can use jquery, and do something like this: `

<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>

`

Give id to Check box

    <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;
           }

this worked for me:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM