用js写出一个学生管理系统
时间: 2023-12-19 12:06:54 浏览: 78
ssm写的简单的学生管理.zip
好的,以下是一个简单的学生管理系统的示例,包括添加学生、编辑学生、删除学生和搜索学生等基本功能。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>学生管理系统</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>学生管理系统</h1>
<form>
<label for="name">姓名:</label>
<input type="text" id="name" required>
<br>
<label for="age">年龄:</label>
<input type="number" id="age" required>
<br>
<label for="gender">性别:</label>
<input type="radio" name="gender" value="男" required>男
<input type="radio" name="gender" value="女" required>女
<br>
<label for="major">专业:</label>
<input type="text" id="major" required>
<br>
<button type="button" onclick="addStudent()">添加学生</button>
<button type="button" onclick="editStudent()">编辑学生</button>
<button type="button" onclick="deleteStudent()">删除学生</button>
<br>
<label for="search">搜索:</label>
<input type="text" id="search">
<button type="button" onclick="searchStudent()">搜索</button>
</form>
<table id="students">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>专业</th>
</tr>
</thead>
<tbody>
<!-- 这里会动态生成学生数据 -->
</tbody>
</table>
<script src="student.js"></script>
</body>
</html>
```
JavaScript 代码:
```javascript
let students = []; // 学生数据数组
function addStudent() {
let name = document.getElementById('name').value;
let age = document.getElementById('age').value;
let gender = document.querySelector('input[name="gender"]:checked').value;
let major = document.getElementById('major').value;
students.push({name, age, gender, major});
renderStudents();
}
function editStudent() {
let index = prompt('请输入要编辑的学生序号:');
if (index !== null && index !== '') {
let name = document.getElementById('name').value;
let age = document.getElementById('age').value;
let gender = document.querySelector('input[name="gender"]:checked').value;
let major = document.getElementById('major').value;
students[index] = {name, age, gender, major};
renderStudents();
}
}
function deleteStudent() {
let index = prompt('请输入要删除的学生序号:');
if (index !== null && index !== '') {
students.splice(index, 1);
renderStudents();
}
}
function searchStudent() {
let keyword = document.getElementById('search').value;
let filteredStudents = students.filter(student => {
return student.name.includes(keyword) || student.major.includes(keyword);
});
renderStudents(filteredStudents);
}
function renderStudents(data = students) {
let tableBody = document.querySelector('#students tbody');
tableBody.innerHTML = '';
data.forEach((student, index) => {
let row = document.createElement('tr');
let nameCell = document.createElement('td');
nameCell.innerText = student.name;
row.appendChild(nameCell);
let ageCell = document.createElement('td');
ageCell.innerText = student.age;
row.appendChild(ageCell);
let genderCell = document.createElement('td');
genderCell.innerText = student.gender;
row.appendChild(genderCell);
let majorCell = document.createElement('td');
majorCell.innerText = student.major;
row.appendChild(majorCell);
let actionCell = document.createElement('td');
let editButton = document.createElement('button');
editButton.innerText = '编辑';
editButton.onclick = () => {
document.getElementById('name').value = student.name;
document.getElementById('age').value = student.age;
if (student.gender === '男') {
document.querySelector('input[name="gender"][value="男"]').checked = true;
} else {
document.querySelector('input[name="gender"][value="女"]').checked = true;
}
document.getElementById('major').value = student.major;
};
actionCell.appendChild(editButton);
let deleteButton = document.createElement('button');
deleteButton.innerText = '删除';
deleteButton.onclick = () => {
if (confirm('确定要删除该学生吗?')) {
students.splice(index, 1);
renderStudents();
}
};
actionCell.appendChild(deleteButton);
row.appendChild(actionCell);
tableBody.appendChild(row);
});
}
```
该学生管理系统使用数组存储学生数据,通过 JavaScript 动态生成表格并实现添加、编辑、删除和搜索等功能。
阅读全文