From 436f897711875fadbf6b499955c8aa55ffb95d8d Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sun, 19 Apr 2026 23:09:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=BC=B9=E6=A1=86=E5=85=B3=E9=97=AD=E7=A1=AE=E8=AE=A4=E5=8F=AA?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E4=B8=80=E6=AC=A1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 定义命名函数作为监听器 - 每次打开编辑弹框时移除旧监听器再添加新监听器 - 确保每次关闭时都能检查是否有修改 --- xian_favor/api.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/xian_favor/api.py b/xian_favor/api.py index 8499559..9930fc1 100644 --- a/xian_favor/api.py +++ b/xian_favor/api.py @@ -2139,6 +2139,15 @@ function openEditModalFromDetail() { } // 打开编辑模态框 +// 编辑弹框关闭确认监听器函数 +function editModalHideHandler(e) { + if (hasEditChanges()) { + if (!confirm('您已修改内容但未保存,是否放弃修改?')) { + e.preventDefault(); // 取消关闭 + } + } +} + async function openEditModal(id) { currentDetailId = id; const res = await fetch(`${API_BASE}/items/${id}`); @@ -2200,17 +2209,14 @@ async function openEditModal(id) { is_starred: item.is_starred }; - const modal = new bootstrap.Modal(document.getElementById('editModal')); - modal.show(); + // 移除旧的监听器,添加新的监听器 + const editModal = document.getElementById('editModal'); + editModal.removeEventListener('hide.bs.modal', editModalHideHandler); + editModal.addEventListener('hide.bs.modal', editModalHideHandler); - // 监听关闭事件,检查是否有修改 - document.getElementById('editModal').addEventListener('hide.bs.modal', function(e) { - if (hasEditChanges()) { - if (!confirm('您已修改内容但未保存,是否放弃修改?')) { - e.preventDefault(); // 取消关闭 - } - } - }, { once: true }); + const modal = new bootstrap.Modal(editModal); + modal.show(); +} } // 检查编辑内容是否有修改