简体   繁体   中英

Checkbox checked property is undefined

I am adding rows and cols to html table programatically using Javascript:

for (var i = 0; i < results.length; i++) {
                    table = document.getElementById("EntityRecords");
                    rowCount = table.rows.length;
                    row = table.insertRow(rowCount);
                    row.id = results[i].LeadId;
                    row.className = "EntityRecordsRow";

                    cell1 = row.insertCell(0);
                    element = document.createElement("input");
                    element.type = "checkbox";
                    element.name = "chkbox[]";
                    cell1.appendChild(element);
                    cell1.className = "EntityRecordsCol";

                    cell2 = row.insertCell(1);
                    cell2.innerHTML = results[i].FirstName + " " + results[i].LastName;
                }

On button click event, I need to find selected checkbox. I am reading the table cells using below code but checkbox cell checked property is undefined:

function OnRecordSelectionBtnClick() {
var table = document.getElementById("EntityRecords");
for (var i = 0, row; row = table.rows[i]; i++) {
    var col = row.cells[0];
    if (col.checked) {
        var selectedText = row.cells[1].innerHTML;
        alert(selectedText);
    }
}
}

Any suggestions?

if ( col.firstElementChild.checked ) , not if ( col.checked ) . col is td element and its first child is input that has checked property.

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