纯html和js的增删改查页面
时间: 2024-02-03 15:14:01 浏览: 59
以下是一个简单的纯HTML和JavaScript实现的增删改查页面示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>增删改查页面</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
border: 1px solid #ccc;
}
th {
background-color: #f2f2f2;
}
form {
display: inline-block;
margin-bottom: 10px;
}
input[type="text"], input[type="submit"] {
padding: 5px;
font-size: 16px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>增删改查页面</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody id="table-body">
<!-- 这里将会动态添加数据 -->
</tbody>
</table>
<form id="add-form">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" required>
<label for="age">年龄:</label>
<input type="text" id="age" name="age" required>
<input type="submit" value="添加">
</form>
<form id="edit-form" style="display: none;">
<label for="edit-name">姓名:</label>
<input type="text" id="edit-name" name="edit-name" required>
<label for="edit-age">年龄:</label>
<input type="text" id="edit-age" name="edit-age" required>
<input type="hidden" id="edit-id" name="edit-id">
<input type="submit" value="保存">
</form>
<script type="text/javascript">
// 模拟从后端获取数据
var data = [
{ id: 1, name: "张三", age: 20 },
{ id: 2, name: "李四", age: 25 },
{ id: 3, name: "王五", age: 30 }
];
// 获取table的tbody元素
var tbody = document.getElementById("table-body");
// 动态生成表格数据
for (var i = 0; i < data.length; i++) {
var tr = document.createElement("tr");
tr.innerHTML = "<td>" + data[i].id + "</td>" +
"<td>" + data[i].name + "</td>" +
"<td>" + data[i].age + "</td>" +
"<td><button onclick='edit(" + data[i].id + ", \"" + data[i].name + "\", " + data[i].age + ")'>编辑</button> <button onclick='remove(" + data[i].id + ")'>删除</button></td>";
tbody.appendChild(tr);
}
// 添加数据
document.getElementById("add-form").addEventListener("submit", function(event) {
event.preventDefault();
var name = document.getElementById("name").value;
var age = parseInt(document.getElementById("age").value);
var id = data.length + 1;
data.push({ id: id, name: name, age: age });
var tr = document.createElement("tr");
tr.innerHTML = "<td>" + id + "</td>" +
"<td>" + name + "</td>" +
"<td>" + age + "</td>" +
"<td><button onclick='edit(" + id + ", \"" + name + "\", " + age + ")'>编辑</button> <button onclick='remove(" + id + ")'>删除</button></td>";
tbody.appendChild(tr);
document.getElementById("add-form").reset();
});
// 编辑数据
function edit(id, name, age) {
document.getElementById("edit-name").value = name;
document.getElementById("edit-age").value = age;
document.getElementById("edit-id").value = id;
document.getElementById("add-form").style.display = "none";
document.getElementById("edit-form").style.display = "inline-block";
}
document.getElementById("edit-form").addEventListener("submit", function(event) {
event.preventDefault();
var name = document.getElementById("edit-name").value;
var age = parseInt(document.getElementById("edit-age").value);
var id = parseInt(document.getElementById("edit-id").value);
for (var i = 0; i < data.length; i++) {
if (data[i].id === id) {
data[i].name = name;
data[i].age = age;
break;
}
}
// 更新表格数据
var trs = tbody.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
if (trs[i].getElementsByTagName("td")[0].innerHTML == id) {
trs[i].getElementsByTagName("td")[1].innerHTML = name;
trs[i].getElementsByTagName("td")[2].innerHTML = age;
break;
}
}
document.getElementById("add-form").style.display = "inline-block";
document.getElementById("edit-form").style.display = "none";
document.getElementById("edit-form").reset();
});
// 删除数据
function remove(id) {
for (var i = 0; i < data.length; i++) {
if (data[i].id === id) {
data.splice(i, 1);
break;
}
}
// 更新表格数据
var trs = tbody.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
if (trs[i].getElementsByTagName("td")[0].innerHTML == id) {
tbody.removeChild(trs[i]);
break;
}
}
}
</script>
</body>
</html>
```
在这个示例中,我们首先定义了一个模拟数据,然后使用JavaScript代码动态生成表格数据,并实现了添加、编辑和删除功能。其中,添加和编辑功能都使用了一个表单,而编辑功能需要动态地将表单更新为原来的数据,并在保存时将修改后的数据更新到模拟数据和表格中。删除功能只需要根据ID找到对应的数据并将其从模拟数据和表格中删除即可。
阅读全文