今天学习了对页面输入内容的约束写法
/ 提交日报逻辑
document.getElementById('submitForm').addEventListener('submit', function (e) {
e.preventDefault();
let batch = document.getElementById('batch').value;
let workerId = document.getElementById('workerId').value;
let workerName = document.getElementById('workerName').value;
let totalTransferred = parseInt(document.getElementById('totalTransferred').value);
let qualifiedTransferred = parseInt(document.getElementById('qualifiedTransferred').value);
let defective = parseInt(document.getElementById('defective').value);
let lost = parseInt(document.getElementById('lost').value);
let reportDate = document.getElementById('reportDate').value;
// 生产批次规则判断
if (!/^\d{10}$/.test(batch)) {
document.getElementById('submitResult').innerHTML = "生产批次格式错误,应为10位数字";
return;
}
let yearMonth = batch.substring(0, 4);
let sequence = batch.substring(4);
if (isNaN(yearMonth) || isNaN(sequence)) {
document.getElementById('submitResult').innerHTML = "生产批次前4位和后4位都应为数字";
return;
}
// 工号规则判断
if (!/^\d{6}$/.test(workerId)) {
document.getElementById('submitResult').innerHTML = "工号格式错误,应为6位数字";
return;
}
let workshop = workerId.substring(0, 3);
let workerSeq = workerId.substring(3);
if (isNaN(workshop) || isNaN(workerSeq)) {
document.getElementById('submitResult').innerHTML = "工号前3位和后3位都应为数字";
return;
}
// 工人姓名规则判断
if (workerName.length > 10) {
document.getElementById('submitResult').innerHTML = "工人姓名不能超过10个汉字";
return;
}
// 日报押平规则判断
if (totalTransferred === qualifiedTransferred + defective + lost) {
// 保存数据到数组,新数据置顶
dailyReports.unshift({
batch: batch,
workerId: workerId,
workerName: workerName,
totalTransferred: totalTransferred,
qualifiedTransferred: qualifiedTransferred,
defective: defective,
lost: lost,
reportDate: reportDate
});
document.getElementById('submitResult').innerHTML = '保存成功';
showPage('queryPage');
} else {
document.getElementById('submitResult').innerHTML = '上报数据有误';
}
});
// 修改日报逻辑
document.getElementById('editForm').addEventListener('submit', function (e) {
e.preventDefault();
let editReportDate = document.getElementById('editReportDate').value;
let today = new Date().toISOString().split('T')[0];
if (editReportDate === today) {
// 查找当天数据
let reportToEdit = dailyReports.find(report => report.reportDate === editReportDate);
if (reportToEdit) {
document.getElementById('editDetails').innerHTML = `生产批次: ${reportToEdit.batch}, 工号: ${reportToEdit.workerId}, 工人姓名: ${reportToEdit.workerName}`;
document.getElementById('editDetailsForm').style.display = 'block';
document.getElementById('editTotalTransferred').value = reportToEdit.totalTransferred;
document.getElementById('editQualifiedTransferred').value = reportToEdit.qualifiedTransferred;
document.getElementById('editDefective').value = reportToEdit.defective;
document.getElementById('editLost').value = reportToEdit.lost;
}
} else {
document.getElementById('editResult').innerHTML = "不允许修改非当天日报";
}
});
document.getElementById('editDetailsForm').addEventListener('submit', function (e) {
e.preventDefault();
let editReportDate = document.getElementById('editReportDate').value;
let reportToEdit = dailyReports.find(report => report.reportDate === editReportDate);
if (reportToEdit) {
reportToEdit.totalTransferred = parseInt(document.getElementById('editTotalTransferred').value);
reportToEdit.qualifiedTransferred = parseInt(document.getElementById('editQualifiedTransferred').value);
reportToEdit.defective = parseInt(document.getElementById('editDefective').value);
reportToEdit.lost = parseInt(document.getElementById('editLost').value);
document.getElementById('editDetailsForm').style.display = 'none';
document.getElementById('editDetails').innerHTML = "";
}
});
标签:10,30,reportToEdit,value,2024,getElementById,let,innerHTML,document
From: https://www.cnblogs.com/dengyizhidai/p/18516761