简体   繁体   English

使用javascript中的复选框删除HTML表格中的多行

[英]Deleting multiple rows in a HTML table using checkbox in javascript

I can't delete the rows that are selected. 我无法删除所选的行。 I want to delete the rows checked with checkbox. 我想删除选中复选框的行。 But I can't access the table rows and check whether the checkbox is checked. 但是我无法访问表行并检查是否选中了复选框。 What should I do? 我该怎么办?

Code: 码:

 <div class="container"> <div class="tab tab-1"> <table id="table" border="1"> <tr> <th></th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>edit</th> </tr> <tr> <td><input type="checkbox" id="select"></td> <td>A1</td> <td>B1</td> <td>10</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" id="select"></td> <td>A3</td> <td>B3</td> <td>30</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" id="select" class="select"></td> <td>A2</td> <td>B2</td> <td>20</td> <td><input type="button" value="edit" id="edit"></td> </tr> </table> </div> <button onclick="deleteRow();">Remove</button> <script> function deleteRow() { var table = document.getElementById("table"); var rowCount = table.rows.length; console.log("rowcount : " + rowCount); for (var i = 1; i < rowCount; i++) { var c = table.rows[i].cells[0].childNodes; console.log(c); if (c.checked == 1) { console.log("i :" + i); table.deleteRow(i); } } }. </script> 

Use querySelectorAll() and :checked to select all checked checkbox. 使用querySelectorAll():checked以选中所有选中的复选框。

parentNode.parentNode is to get parent tr node parentNode.parentNode是获取父tr节点

 function deleteRow() { document.querySelectorAll('#table .select:checked').forEach(e => { e.parentNode.parentNode.remove() }); } 
 <div class="container"> <div class="tab tab-1"> <table id="table" border="1"> <tr> <th></th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>edit</th> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A1</td> <td>B1</td> <td>10</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A3</td> <td>B3</td> <td>30</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A2</td> <td>B2</td> <td>20</td> <td><input type="button" value="edit" id="edit"></td> </tr> </table> </div> </div> <button onclick="deleteRow();">Remove</button> 

Note: Avoid using duplicate id 注意:避免使用重复的id

The problem with your code is that after deleting a row, the index of all subsequent rows immediately decreases by one because HTMLTableElement.prototype.rows returns a live HTMLCollection . 代码的问题在于删除行后,所有后续行的索引会立即减1,因为HTMLTableElement.prototype.rows返回实时 HTMLCollection

That not only leads to your loop executing too often (because you cached table.rows.length ), but also to subsequent indexes no longer matching. 这不仅导致您的循环执行过于频繁(因为您缓存了table.rows.length ),而且还导致后续索引不再匹配。

I'd suggest using the much more readable for...of loop, which only gets slightly more complicated because tables don't seem to allow for using table.removeChild(row) : 我建议使用更易读for...of循环,这只会稍微复杂一些,因为表似乎不允许使用table.removeChild(row)

 function deleteRow() { const table = document.getElementById("table"); for (const [index, row] of [...table.rows].entries()) { if (row.querySelector('input:checked')) { table.deleteRow(index); } } } 
 <table id="table" border="1"> <tr> <th></th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>edit</th> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A1</td> <td>B1</td> <td>10</td> <td><input type="button" value="edit" class="edit"></td> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A3</td> <td>B3</td> <td>30</td> <td><input type="button" value="edit" class="edit"></td> </tr> <tr> <td><input type="checkbox" class="select"></td> <td>A2</td> <td>B2</td> <td>20</td> <td><input type="button" value="edit" class="edit"></td> </tr> </table> <button onclick="deleteRow();">Remove</button> 

The way you go about deleting the rows is wrong, because you use a for loop with a pre-cached length, but when a row, say i , is deleted then the row i+1 becomes i and the length changes. 删除行的方法是错误的,因为你使用了一个具有预缓存长度的for循环,但是当一行(比如i )被删除时,行i+1变为i并且长度发生变化。

A correct way to solve this problem is to use a while loop without caching the number of rows and incrementing the index only when you don't delete a row. 解决此问题的正确方法是使用while循环而不缓存行数并仅在不删除行时递增索引。

Example: 例:

 function deleteRow () { /* Cache the table. */ var table = document.getElementById("table"); /* Create the index used in the loop. */ var index = 1; /* Repeat as long as the index is less than the number of rows. */ while (index < table.rows.length) { /* Get the input of the cell. */ var input = table.rows[index].cells[0].children[0]; /* Check whether the input exists and is checked. */ if (input && input.checked) { /* Delete the row at the current index. */ table.deleteRow(index); } else { /* Increment the index. */ index++; } } } 
 <div class="container"> <div class="tab tab-1"> <table id="table" border="1"> <tr> <th></th> <th>First Name</th> <th>Last Name</th> <th>Age</th> <th>edit</th> </tr> <tr> <td><input type="checkbox" id="select"></td> <td>A1</td> <td>B1</td> <td>10</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" id="select"></td> <td>A3</td> <td>B3</td> <td>30</td> <td><input type="button" value="edit" id="edit"></td> </tr> <tr> <td><input type="checkbox" id="select" class="select"></td> <td>A2</td> <td>B2</td> <td>20</td> <td><input type="button" value="edit" id="edit"></td> </tr> </table> </div> <button onclick="deleteRow();">Remove</button> 

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

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