<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
a {
text-decoration: none;
}
.newAdd {
width: 800px;
height: 110px;
text-align: center;
margin: 0 auto;
}
.newAdd h2 {
padding: 10px;
}
.newAdd .info input {
width: 60px;
outline: none;
}
.employment {
width: 600px;
margin: 0 auto;
text-align: center;
}
.employment th,
.employment td {
padding: 5px 10px;
}
.employment caption {
padding: 10px;
font-weight: 700;
font-size: 20px;
}
</style>
</head>
<body>
<form class="newAdd" autocomplete="off">
<h2>新增学员</h2>
<div class="info">
姓名:<input type="text" class="uname" name="">
年龄:<input type="text" class="age" name="">
性别:<select name="" id="" class="sex" name="">
<option value="男" selected>男</option>
<option value="女">女</option>
</select>
薪资:<input type="text" class="salary" name="">
就业城市:<select name="" id="" class="city" name="">
<option value="北京" selected>北京</option>
<option value="河南">河南</option>
<option value="河北">河北</option>
<option value="南京">南京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="盛京">盛京</option>
</select>
<button>录入</button>
</div>
</form>
<!-- 就业榜 -->
<table class="employment" border="1" cellpadding="10" cellspacing="0">
<caption>就业榜</caption>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<!-- <td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td> -->
</tr>
</tbody>
</table>
<script>
const uname = document.querySelector('.uname');
const age = document.querySelector('.age');
const sex = document.querySelector('.sex');
const salary = document.querySelector('.salary');
const city = document.querySelector('.city');
const tbody = document.querySelector('.employment tbody');
const arr = [];
const newAdd = document.querySelector('.newAdd');
console.log(newAdd);
newAdd.addEventListener('submit', function (e) {
e.preventDefault();
const item = document.querySelectorAll('[name]');
for (let i = 0; i < item.length; i++) {
if (item[i].value === '') {
return alert('输入内容不能为空');
}
}
const obj = {
xuHao: arr.length + 1,
uname: uname.value,
age: age.value,
sex: sex.value,
salary: salary.value,
city: city.value,
}
arr.push(obj);
console.log(arr);
newAdd.reset();
reder();
})
function reder() {
tbody.innerHTML = '';
for (let i = 0; i < arr.length; i++) {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${arr[i].xuHao}</td>
<td>${arr[i].uname}</td>
<td>${arr[i].age}</td>
<td>${arr[i].sex}</td>
<td>${arr[i].salary}</td>
<td>${arr[i].city}</td>
<td><a href="#" data-id=${i}>删除</a></td>
`;
tbody.appendChild(tr);
}
}
tbody.addEventListener('click', function (e) {
if (e.target.tagName = "A") {
arr.splice(e.target.dataset.id, 1);
reder();
}
})
</script>
</body>
</html>
