AI 生成: 一个基于 Python Flask 的车辆管理系统,提供车辆的增删改查、搜索和统计功能。
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/* 车辆管理系统前端脚本 */
|
||||
|
||||
// 打开添加弹窗
|
||||
function openAddModal() {
|
||||
document.getElementById('addModal').classList.add('show');
|
||||
}
|
||||
|
||||
// 关闭添加弹窗
|
||||
function closeAddModal() {
|
||||
document.getElementById('addModal').classList.remove('show');
|
||||
document.getElementById('addForm').reset();
|
||||
}
|
||||
|
||||
// 提交添加表单
|
||||
async function submitAdd(e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const data = Object.fromEntries(new FormData(form).entries());
|
||||
// 年份转数字
|
||||
if (data.year) data.year = parseInt(data.year, 10);
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/vehicles', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
const result = await res.json();
|
||||
if (!res.ok) {
|
||||
alert('添加失败: ' + (result.error || '未知错误'));
|
||||
return;
|
||||
}
|
||||
location.reload();
|
||||
} catch (err) {
|
||||
alert('请求失败: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除车辆
|
||||
async function deleteVehicle(id) {
|
||||
if (!confirm('确定要删除该车辆吗?')) return;
|
||||
try {
|
||||
const res = await fetch(`/api/vehicles/${id}`, { method: 'DELETE' });
|
||||
if (!res.ok) {
|
||||
alert('删除失败');
|
||||
return;
|
||||
}
|
||||
location.reload();
|
||||
} catch (err) {
|
||||
alert('请求失败: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 提交编辑表单
|
||||
async function submitEdit(e, id) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const data = Object.fromEntries(new FormData(form).entries());
|
||||
if (data.year) data.year = parseInt(data.year, 10);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/vehicles/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
const result = await res.json();
|
||||
if (!res.ok) {
|
||||
alert('更新失败: ' + (result.error || '未知错误'));
|
||||
return;
|
||||
}
|
||||
window.location.href = '/';
|
||||
} catch (err) {
|
||||
alert('请求失败: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 点击弹窗外部关闭
|
||||
window.addEventListener('click', function (e) {
|
||||
const modal = document.getElementById('addModal');
|
||||
if (modal && e.target === modal) closeAddModal();
|
||||
});
|
||||
Reference in New Issue
Block a user