Update answer.html

main
luoyonghuang 2 months ago
parent f940e22722
commit 06e46f5383

@ -47,40 +47,153 @@
<body>
<div class="container">
<div id="question">你需要回答问题 结果是:</div>
<button id="correctButton">正确</button>
<button id="wrongButton">错误</button>
<div id="question">请重复问题,选择正确或错误:</div>
<button id="repeatCorrectButton">准确重复</button>
<button id="repeatWrongButton">未准确重复</button>
<div id="questionResult"></div> <!-- 显示重复问题的结果 -->
<div id="answerContainer" style="display:none;">
<p>现在回答问题:</p>
<button id="correctButton">回答正确</button>
<button id="partiallyCorrectButton">部分正确</button>
<button id="wrongButton">回答错误</button>
</div>
<button class="back-button" onclick="window.location.href='select.html'">返回点名页面</button>
<div id="result"></div>
<div id="studentInfo"></div> <!-- 显示学生信息 -->
</div>
<script>
async function updateScore(student_id, points) {
try {
const response = await fetch('/api/update-score', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ student_id, points }),
});
if (!response.ok) {
throw new Error('网络响应不正常');
}
const data = await response.text(); // 使用 text() 解析纯数字响应
console.log('更新后的分数:', data); // data 是字符串形式的分数
return data; // 返回更新后的分数字符串
} catch (error) {
console.error('分数更新失败:', error);
return null;
}
}
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get('name');
const student_name = urlParams.get('student_name');
const student_id = urlParams.get('student_id');
const repeatCorrectButton = document.querySelector('#repeatCorrectButton');
const repeatWrongButton = document.querySelector('#repeatWrongButton');
const questionResultDisplay = document.querySelector('#questionResult');
const correctButton = document.querySelector('#correctButton');
const partiallyCorrectButton = document.querySelector('#partiallyCorrectButton');
const wrongButton = document.querySelector('#wrongButton');
const resultDisplay = document.querySelector('#result');
const answerContainer = document.querySelector('#answerContainer');
const studentInfoDisplay = document.querySelector('#studentInfo');
// 显示学生姓名和学号
studentInfoDisplay.textContent = `学生: ${student_name} 学号: ${student_id}`;
// 正确重复问题处理 +0.5
repeatCorrectButton.addEventListener('click', () => {
updateScore(student_id, 0.5)
.then(newscore => {
if (newscore !== null) {
questionResultDisplay.textContent = `重复正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
answerContainer.style.display = 'block'; // 显示回答问题部分
} else {
console.error('更新分数失败');
questionResultDisplay.textContent = `更新分数失败,请重试。`;
}
})
.catch(error => {
console.error('请求失败:', error);
questionResultDisplay.textContent = `更新分数失败,请重试。`;
});
});
// 初始化积分
let userPoints = parseInt(localStorage.getItem(name));
// 未准确重复问题处理 -1
repeatWrongButton.addEventListener('click', () => {
updateScore(student_id, -1)
.then(newscore => {
if (newscore !== null) {
questionResultDisplay.textContent = `重复错误!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
answerContainer.style.display = 'block'; // 显示回答问题部分
} else {
console.error('更新分数失败');
questionResultDisplay.textContent = `更新分数失败,请重试。`;
}
})
.catch(error => {
console.error('请求失败:', error);
questionResultDisplay.textContent = `更新分数失败,请重试。`;
});
});
// 正确答案处理
// 回答问题的处理
correctButton.addEventListener('click', () => {
userPoints += 10; // 正确回答加 10 分
localStorage.setItem(name, userPoints); // 保存更新后的积分
resultDisplay.textContent = `正确!${name} 的当前积分: ${userPoints}`;
updateScore(student_id, 3)
.then(newscore => {
if (newscore !== null) {
resultDisplay.textContent = `回答完全正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
} else {
console.error('更新分数失败');
resultDisplay.textContent = `更新分数失败,请重试。`;
}
})
.catch(error => {
console.error('请求失败:', error);
resultDisplay.textContent = `更新分数失败,请重试。`;
});
});
partiallyCorrectButton.addEventListener('click', () => {
updateScore(student_id, 1.5)
.then(newscore => {
if (newscore !== null) {
resultDisplay.textContent = `回答部分正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
} else {
console.error('更新分数失败');
resultDisplay.textContent = `更新分数失败,请重试。`;
}
})
.catch(error => {
console.error('请求失败:', error);
resultDisplay.textContent = `更新分数失败,请重试。`;
});
});
// 错误答案处理
wrongButton.addEventListener('click', () => {
userPoints -= 5;
localStorage.setItem(name, userPoints); // 保存更新后的积分
resultDisplay.textContent = `错误!${name} 的当前积分: ${userPoints}`;
wrongButton.addEventListener('click', () => {
updateScore(student_id, 0.5)
.then(newscore => {
if (newscore !== null) {
resultDisplay.textContent = `回答基本正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
} else {
console.error('更新分数失败');
resultDisplay.textContent = `更新分数失败,请重试。`;
}
})
.catch(error => {
console.error('请求失败:', error);
resultDisplay.textContent = `更新分数失败,请重试。`;
});
});
</script>
</body>
</html>

Loading…
Cancel
Save