2013-08-28 47 views
3

我有一個包含複選框的列的HTML表格。任何人都可以請告訴我如何使用Javascript來設置複選框在特定的行索引進行檢查(不使用jQuery)?檢查HTML表格中的特定複選框

感謝

+0

爲什麼你不想使用jQuery?它的打字少得多,而且學習/使用更簡單。這些介紹值得你花時間:[thenewboston.com](http://thenewboston.org/list.php?cat=32)和[phpacademy.org](https://phpacademy.org/courses/javascript-jquery) – gibberish

回答

0

最簡單的辦法是給每個checkbok一個特定ID。這樣很容易用javascript來檢查它。

可能最好的解決方案是給它一個包含行號或行中數據的唯一標識符的id。

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在那裏你也可以遍歷cellschildren陣列明確地尋找checkbox類型的input元素。沒有真正看到你的代碼很難給出具體的建議,但希望這有助於。

相關問題