基于JavaWeb的学生管理系统
- 这是我web课程的一个小作业,可以拿这个练练手,超简单~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>学生管理系统</title>
</head>
<style>
body{background-color: antiquewhite};
.btn button{
margin-top: 20px;
width: 90px;
height: 40px;
color: white;
border: 0;
border-radius: 10px;
font-size: 17px;
font-weight: bold;
background-color: transparent;
margin-bottom: 23px;
}
.btn button:hover{
background-color: #FAD961;
background-image: linear-gradient(90deg, #FAD961 0%, #F76B1C 100%);
}
#addstu{
margin-left: 30px;
}
</style>
<body >
<center>
<h1>学生管理系统</h1>
<p>学生姓名:<input type="text" id="name"></p>
<p>学生年龄:<input type="text" id="age"></p>
<p>学生性别:<input type="radio" name="sex" id="male" checked value="男">男<input type="radio" id="female" name="sex" value="女">女</p>
<p>考试成绩:<input type="text" id="score"></p>
<div class="btn">
<button id="addstu">添加学生</button><br><br>
</div>
<table id="stuInfo" border="" cellspacing="0" style="text-align: center"></table>
<script>
let tab=document.getElementById("stuInfo")
let addStu=document.getElementById("addstu");
let name=document.getElementById("name");
let age=document.getElementById("age");
let score=document.getElementById("score");
let sex=document.getElementsByName("sex");
let index=null;
let stuInfo=null;
let render=function(stuInfo){
tab.innerHTML='';
if(stuInfo.length!==0){
let thead='<tr><td><b>学生姓名</b></td><td><b>学生年龄</b></td><td><b>学生性别</b></td><td><b>考试成绩</b></td><td><b>操作</b></td></tr>'
let tbody='';
for(let i=0;i<stuInfo.length;i++){
tbody+=`<tr>
<td>${stuInfo[i].name}</td>
<td>${stuInfo[i].age}</td>
<td>${stuInfo[i].sex}</td>
<td>${stuInfo[i].score}</td>
<td><button οnclick=editStu(${i})>修改</button><button οnclick=delStu(${i})>删除</button></td>
</tr>`
}
tab.innerHTML+=thead+tbody;
}
else{
tab.innerHTML='';
}
}
let typeChange=function(a){
if(Array.isArray(a)){
let obj={};
for(let key in a){
obj[key]=a[key];
}
return obj;
}
else{
let arr=[];
for(let key in a){
arr[key]=a[key];
}
return arr;
}
}
window.onload=function(){
if(localStorage.stuInfo===undefined){
let info={
0:{'name':'海绵宝宝','age':15,'sex':'男','score':90},
1:{'name':'派大星','age':15,'sex':'男','score':80},
2:{'name':'蟹老板','age':30,'sex':'男','score':45}
}
localStorage.stuInfo=JSON.stringify(info);
stuInfo=typeChange(info)
}
else{
stuInfo=typeChange(JSON.parse(localStorage.stuInfo))
}
render(stuInfo);
}
let makeNewStu=function(){
let sexValue=null;
for(let i=0;i<sex.length;i++){
if(sex[i].checked===true){
sexValue=sex[i].value;
}
}
let newStu={'name':name.value,'age':age.value,'sex':sexValue,'score':score.value};
return newStu;
}
addStu.addEventListener('click',function(){
if(addStu.innerHTML==='添加学生'){
if(name.value===''||age.value===''||score.value===''){
alert('信息不能为空');
}
else{
let newStu=makeNewStu();
stuInfo.push(newStu);
render(stuInfo);
localStorage.stuInfo=JSON.stringify(typeChange(stuInfo));
name.value="";
age.value="";
sex[0].checked=true;
score.value="";
}
}
else{
let newInfo=makeNewStu();
stuInfo.splice(index,1,newInfo);
render(stuInfo);
localStorage.stuInfo=JSON.stringify(typeChange(stuInfo));
addStu.innerHTML='添加学生';
name.value="";
age.value="";
score.value="";
sex[0].checked=true;
}
},false)
let delStu=function(i){
if(window.confirm('确定是否删除此学生')){
stuInfo.splice(i,1);
render(stuInfo);
localStorage.stuInfo=JSON.stringify(typeChange(stuInfo));
}
}
let editStu=function(i){
addStu.innerHTML='确定修改';
name.value=stuInfo[i].name;
age.value=stuInfo[i].age;
if(stuInfo[i].sex==='男'){
sex[0].checked=true;
}
else{
sex[1].checked=true
}
score.value=stuInfo[i].score;
index=i
}
</script>
</center>
</body>
</html>