简体   繁体   English

复选框已选中属性未定义

[英]Checkbox checked property is undefined

I am adding rows and cols to html table programatically using Javascript: 我使用Javascript以编程方式将行和列添加到html表:

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: 我正在使用下面的代码读取表格单元格,但复选框单元格checked属性未定义:

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 ) . if ( col.firstElementChild.checked ) ,而不是if ( col.checked ) col is td element and its first child is input that has checked property. col是td元素,它的第一个子元素是已checked属性的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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