.Net待办表新增卡片功能运行异常,无法定位问题求助
待办卡片新增功能问题修复方案
已发现错误点
- 变量声明语法错误:
getvalue3变量定义行末尾没有加逗号分隔后续变量,直接导致JS解析报错,逻辑中断 - 插入父级选择错误:当前代码选择
.drag-1(已有待办卡片的类名)作为插入容器,会导致新卡片嵌套进旧卡片内部,不符合需求 - DOM结构语法错误:新增卡片的HTML字符串存在标签嵌套混乱、属性书写错误(
alt'=P-'、rounded - circle多空格)、标签未闭合的问题 - 重复ID问题:所有新增元素都使用了固定ID,DOM中ID必须唯一,会导致后续元素选择、事件绑定全部异常
- 代码结构不完整:你提供的JS代码片段没有闭合
append方法的引号、事件回调函数以及$(document).ready方法,直接运行会报语法错误 - 未阻止表单默认行为:如果提交按钮放在form标签内,点击后会触发默认表单提交刷新页面,导致插入逻辑还未执行就被中断
修复后代码示例
JavaScript代码
// 新增待办卡片功能 $(document).ready(function () { classnames(); // 初始化dragula拖拽 dragula([ document.getElementById('tasks-upcoming'), document.getElementById('tasks-progress'), document.getElementById('tasks-completed'), document.getElementById('skill') ]).on('drop', function (el) { classnames(); }); // 提交新增卡片 $(".submit").click(function (e) { // 阻止默认提交行为 e.preventDefault(); const taskName = document.getElementById("taskName").value, informationText = document.getElementById("informationText").value, skill = document.getElementById("skill").value, // 生成唯一ID避免重复 uuid = Date.now(); // 拼接和原结构一致的卡片HTML const newCard = ` <div class="card mb-3 bg-light cursor-grab border drag-1" id="task-${uuid}"> <div class="card-body p-3"> <div class="float-end mr-n2"> <label class="form-check"> <input type="checkbox" class="form-check-input" aria-label="completed" checked> </label> </div> <p class="worktitle">${taskName}</p> <div class="float-end mt-n1"> <img class="userName" src="../images/DefaultUser-50.png" width="32" height="32" class="rounded-circle" alt="Avatar"> </div> <a class="workShow ist-group-item active waves-effect btn-primary btn-sm" data-toggle="modal" data-target="#ModalLong">Show</a> <p class="work-info d-none">${informationText}</p> <p class="skill-info d-none">${skill}</p> </div> </div>`; // 插入到待办列表容器中 $("#tasks-upcoming").append(newCard); // 插入成功后关闭模态框、清空表单 $("#ModalLong").modal('hide'); document.getElementById("taskName").value = ''; document.getElementById("informationText").value = ''; document.getElementById("skill").value = ''; }); });
补充说明
原有代码中把模态框的textBody内容直接塞到卡片里的逻辑是错误的,你可以把需要存储的额外信息用类名为d-none的隐藏元素存在卡片里,点击Show按钮的时候再读取对应卡片的隐藏内容渲染到模态框中即可。
内容的提问来源于stack exchange,提问作者Murat Korkmaz
相关产品推荐
相关产品推荐

