html 构建一个14列10行的表格,每一行有一个复选框,构建三个按钮,增加,删除和保存,增加按钮点击,可以追加一行,点击删除按钮,删除被复选的行的全部数据,点击保存按钮,保存这个表格的数据给服务器
时间: 2024-10-03 20:01:33 浏览: 50
要在HTML中构建一个14列10行的表格,包含复选框和三个按钮,你可以按照以下步骤编写代码:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>表格管理</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: left;
}
input[type=checkbox] {
margin-right: 5px;
}
button {
margin-top: 5px;
}
</style>
</head>
<body>
<table id="myTable">
<thead>
<tr>
<th>列1</th>
<!-- ... 继续添加到第14列 -->
<th>列14</th>
</tr>
</thead>
<tbody>
<!-- 这里通过JavaScript动态生成10行 -->
<% for (let i = 0; i < 10; i++) { %>
<tr>
<td><input type="checkbox" class="row-check"></td>
<!-- ... 在每个单元格填写内容 -->
<% for (let j = 1; j <= 14; j++) { %>
<td>数据%d</td> <!-- 可以为真实的值或占位符 -->
<% } %>
</tr>
<% } %>
</tbody>
<tfoot>
<tr>
<td colspan="14">
<button onclick="addRow()">增加</button>
<button onclick="deleteCheckedRows()">删除已选</button>
<button onclick="saveData()">保存</button>
</td>
</tr>
</tfoot>
</table>
<script>
function addRow() {
const newRow = `
<tr>
<td><input type="checkbox" class="row-check"></td>
<!-- ... 按照需要复制前14列 -->
</tr>
`;
const tbody = document.querySelector('#myTable tbody');
tbody.insertAdjacentHTML('beforeend', newRow);
}
function deleteCheckedRows() {
const checkboxes = document.querySelectorAll('#myTable .row-check:checked');
checkboxes.forEach((checkbox) => {
checkbox.parentElement.remove();
});
}
function saveData() {
// 获取所有选中的行数据
const selectedRows = Array.from(document.querySelectorAll('#myTable tr:not(:first-child):not(:last-child)'))
.filter(row => row.querySelector('.row-check:checked'));
// 创建一个POST请求对象
const formData = new FormData();
selectedRows.forEach(row => {
let rowData = Array.from(row.cells)
.map(cell => cell.textContent || '').join(',');
formData.append('data[]', rowData);
});
fetch('/api/save', {method: 'POST', body: formData})
.then(response => response.text())
.then(result => console.log('Save result:', result))
.catch(error => console.error('Error saving data:', error));
}
</script>
</body>
</html>
```
注意:这里使用了模板字符串和自执行函数(`<% %>` 和 `<script>` 内部),这是ASP.NET MVC语法的一部分,如果你是在纯HTML环境中,你需要使用JavaScript或者其他模板引擎(如EJS、Handlebars等)来替换这部分。
阅读全文
相关推荐
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)