检查HTML表格中的特定复选框

use*_*581 3 html javascript checkbox html-table

我有一个HTML表,其中包含一个包含复选框的列.任何人都可以告诉我如何使用Javascript设置特定行索引的复选框来检查(不使用jquery)?

谢谢

小智 7

如果您的复选框具有唯一ID,则可以执行以下操作:

// To check the box with id "check3"
document.getElementById("check3").checked = true;
Run Code Online (Sandbox Code Playgroud)

但是,如果你真的想根据表中的行索引来检查它,那么你必须首先抓住表并从那里下拉到特定的行索引.

// To check the box in row 3 in table with id "mytable" (0-based row index)
// assuming that the checkbox is in the first column, 
// and that it is the first element in that column
document.getElementById("mytable").rows[2].cells[0].children[0].checked = true;
Run Code Online (Sandbox Code Playgroud)

您可以通过明确查找类型元素的cellschildren数组循环,而不是对那里的0进行硬编码.如果没有真正看到您的代码,很难给出具体的建议,但希望这会有所帮助.inputcheckbox