成绩录入页面编写: 点击"添加"按纽时完成成绩录入,并计算语文,数学,总分最高分,总评等级 总评等级按平均分计算,90及以上为优秀,80良好,70中等,60及格,60以下为不及格
时间: 2023-09-30 15:06:04 浏览: 100
好的,针对你的需求,我给出一个完整的示例代码,包括HTML、CSS和JavaScript。这里使用了jQuery库来简化代码。
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成绩录入</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id="grade-form">
<label for="name">姓名:</label>
<input type="text" id="name" name="name">
<br>
<label for="math-grade">数学成绩:</label>
<input type="number" id="math-grade" name="math-grade">
<br>
<label for="english-grade">英语成绩:</label>
<input type="number" id="english-grade" name="english-grade">
<br>
<label for="chinese-grade">语文成绩:</label>
<input type="number" id="chinese-grade" name="chinese-grade">
<br>
<input type="submit" value="添加">
</form>
<table id="grade-table">
<thead>
<tr>
<th>姓名</th>
<th>数学</th>
<th>英语</th>
<th>语文</th>
<th>总分</th>
<th>总评等级</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<td colspan="5">最高分:</td>
<td id="highest-grade"></td>
</tr>
<tr>
<td colspan="5">总评等级:</td>
<td id="average-grade"></td>
</tr>
</tfoot>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
form {
margin-bottom: 20px;
}
label {
display: inline-block;
width: 100px;
font-weight: bold;
margin-bottom: 10px;
}
input[type="text"],
input[type="number"] {
display: inline-block;
width: 200px;
height: 25px;
border: 1px solid #ccc;
border-radius: 3px;
padding: 5px;
margin-bottom: 10px;
}
input[type="submit"] {
display: block;
width: 100px;
height: 30px;
border: none;
background-color: #007bff;
color: #fff;
border-radius: 3px;
cursor: pointer;
}
table {
border-collapse: collapse;
border: 1px solid #ccc;
margin-bottom: 20px;
}
th, td {
padding: 10px;
border: 1px solid #ccc;
text-align: center;
}
tfoot td {
font-weight: bold;
}
```
JavaScript代码:
```javascript
$(document).ready(function() {
// 初始化成绩数组和统计变量
var grades = [];
var highestGrade = 0, averageGrade = 0;
// 表单提交事件处理函数
$('#grade-form').submit(function(event) {
event.preventDefault();
// 获取表单数据
var name = $('#name').val();
var mathGrade = parseInt($('#math-grade').val());
var englishGrade = parseInt($('#english-grade').val());
var chineseGrade = parseInt($('#chinese-grade').val());
// 计算总分和总评等级
var totalGrade = mathGrade + englishGrade + chineseGrade;
var averageGrade = totalGrade / 3;
var level = '';
if (averageGrade >= 90) {
level = '优秀';
} else if (averageGrade >= 80) {
level = '良好';
} else if (averageGrade >= 70) {
level = '中等';
} else if (averageGrade >= 60) {
level = '及格';
} else {
level = '不及格';
}
// 添加到成绩数组中
var grade = {
name: name,
mathGrade: mathGrade,
englishGrade: englishGrade,
chineseGrade: chineseGrade,
totalGrade: totalGrade,
level: level
};
grades.push(grade);
// 更新表格和统计信息
updateTable();
updateStatistics();
});
// 更新表格函数
function updateTable() {
var tbodyHtml = '';
for (var i = 0; i < grades.length; i++) {
var grade = grades[i];
tbodyHtml += '<tr>' +
'<td>' + grade.name + '</td>' +
'<td>' + grade.mathGrade + '</td>' +
'<td>' + grade.englishGrade + '</td>' +
'<td>' + grade.chineseGrade + '</td>' +
'<td>' + grade.totalGrade + '</td>' +
'<td>' + grade.level + '</td>' +
'</tr>';
}
$('#grade-table tbody').html(tbodyHtml);
}
// 更新统计信息函数
function updateStatistics() {
// 计算最高分
var maxTotalGrade = 0;
var maxGrade = null;
for (var i = 0; i < grades.length; i++) {
var grade = grades[i];
if (grade.totalGrade > maxTotalGrade) {
maxTotalGrade = grade.totalGrade;
maxGrade = grade;
}
}
highestGrade = maxTotalGrade;
$('#highest-grade').html(maxGrade ? maxGrade.name + '(' + maxTotalGrade + '分)' : '');
// 计算平均分和总评等级
var totalGrade = 0;
for (var i = 0; i < grades.length; i++) {
var grade = grades[i];
totalGrade += grade.totalGrade;
}
if (grades.length > 0) {
averageGrade = totalGrade / grades.length;
} else {
averageGrade = 0;
}
var level = '';
if (averageGrade >= 90) {
level = '优秀';
} else if (averageGrade >= 80) {
level = '良好';
} else if (averageGrade >= 70) {
level = '中等';
} else if (averageGrade >= 60) {
level = '及格';
} else {
level = '不及格';
}
$('#average-grade').html(level + '(' + averageGrade.toFixed(2) + '分)');
}
});
```
通过以上代码,当用户填写完成绩并点击“添加”按钮时,会将成绩数据存储在一个JavaScript数组中,并更新表格和统计信息。同时,会根据平均分计算总评等级。最后,会在表格底部显示最高分和总评等级。
阅读全文