2

我有一个 HTML 表格,其中有一列包含复选框。谁能告诉我如何使用Javascript在特定行索引处设置一个复选框以检查(不使用jquery)?

谢谢

4

3 回答 3

7

如果您的复选框具有唯一的 ID,那么您可以简单地执行以下操作:

// To check the box with id "check3"
document.getElementById("check3").checked = true;

但是,如果您确实想根据表中的行索引来检查它,那么您必须首先抓取表并从那里下拉到特定的行索引。

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

除了在其中对 0 进行硬编码,您还可以循环遍历cellsandchildren数组,显式查找inputtype 的元素checkbox。如果没有实际看到您的代码,很难给出具体的建议,但希望这会有所帮助。

于 2013-08-28T18:29:19.367 回答
0

最简单的解决方案是给每个 checkbok 一个特定的 id。像这样很容易用javascript检查它。

最好的解决方案可能是给它一个包含行号或行中数据的唯一标识符的 id。

于 2013-08-28T17:51:29.720 回答
0

这是一个检查\取消选中所有复选框的功能。

http://www.webdevdoor.com/javascript-ajax/javascript-function-check-uncheck-checkboxes/

复选框应该有一个 ID,然后您可以使用“getElementById()”获取它们

于 2013-08-28T17:53:27.940 回答