from flask import jsonify
def manage_score_student_add_up():
score = request.form['formDataString']
config_id = request.form['config_id']
classValue = request.form['classValue']
nameValue = request.form['nameValue'].replace(" ", "")
kaohaoValue = request.form['kaohaoValue'].replace(" ", "")
grade_id = request.form['grade_id']
print(score)
total_score = 0
for item in score.split(','):
key, value = item.split(':')
total_score = total_score + float(value)
if kaohaoValue and nameValue and classValue:
is_have_this_score = Score.query.filter_by(config_id=config_id, test_number=kaohaoValue).first()
if not is_have_this_score:
new_score = Score(
test_number=kaohaoValue,
student_name=nameValue,
grade_id=grade_id,
class_id=classValue,
score=score,
total_score=total_score,
config_id=config_id
)
this_score_upload = Score_upload.query.filter_by(config_id=config_id,
banji_id=classValue).first()
count = this_score_upload.count
this_score_upload.count = count + 1
db.session.add(new_score)
db.session.commit()
return jsonify({'message': 'success'}), 200
else:
return jsonify({'error': '考试已有该考号的考生成绩!'}), 400
else:
return jsonify({'error': '请将所有项目填写完整!'}), 400
<script>
$(document).ready(function() {
$('#score_submit').on('click', function(event) {
event.preventDefault();
var config_id = {{ config_id }};
var grade_id = {{ grade_id }};
var formData = [];
// 获取班级、学生姓名和学生考号的值
var classValue = $('#add_class').val(); // 班级
var nameValue = $('#add_name').val(); // 学生姓名
var kaohaoValue = $('#add_kaohao').val(); // 学生考号
// 遍历其他输入类型为数字的输入框
$('#scoreForm input[type="number"]').each(function() {
var id = $(this).attr('id');
var value = $(this).val();
formData.push(id + ':' + value);
});
var formDataString = formData.join(',');
$.ajax({
url: '/manage/score/student/add/up/',
type: 'POST',
data: {
formDataString: formDataString,
config_id: config_id,
grade_id: grade_id,
classValue: classValue,
nameValue: nameValue,
kaohaoValue: kaohaoValue
},
success: function(response) {
alertify.alert("数据提交成功!");
window.parent.$('#myModal').modal('hide'); // 关闭父页面的模态框
window.parent.location.reload(); // 刷新当前页面
},
error: function(xhr) {
// 解析后端返回的错误信息
let errorMessage = '';
try {
const response = JSON.parse(xhr.responseText);
errorMessage = response.error || "发生未知错误";
} catch (e) {
errorMessage = "发生未知错误";
}
console.error("错误: " + errorMessage);
alertify.alert(errorMessage);
}
});
});
});
</script>
本文转载自互联网,如有侵权,联系删除。