在JavaScript中,动态删除表格(table)中的选中行通常涉及到以下几个步骤:
querySelectorAll
)来获取需要删除的行。以下是一个简单的示例,展示如何通过JavaScript动态删除选中的表格行:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Row Deletion</title>
</head>
<body>
<table id="myTable" border="1">
<tr>
<th>Select</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td><input type="checkbox" class="rowCheckbox"></td>
<td>John Doe</td>
<td>25</td>
</tr>
<tr>
<td><input type="checkbox" class="rowCheckbox"></td>
<td>Jane Smith</td>
<td>30</td>
</tr>
<!-- More rows can be added here -->
</table>
<button onclick="deleteSelectedRows()">Delete Selected Rows</button>
<script>
function deleteSelectedRows() {
// Get all checkboxes in the table
const checkboxes = document.querySelectorAll('#myTable .rowCheckbox');
checkboxes.forEach((checkbox, index) => {
if (checkbox.checked) {
// Get the row of the checked checkbox
const row = checkbox.closest('tr');
// Remove the row from the table
row.parentNode.removeChild(row);
}
});
}
</script>
</body>
</html>
通过以上步骤和示例代码,你可以实现JavaScript动态删除选中行的功能,并解决可能遇到的问题。
领取专属 10元无门槛券
手把手带您无忧上云