【发布时间】:2019-05-05 17:46:36
【问题描述】:
我的删除按钮无法正常工作。我有一个有 4 行的表,还有一个删除按钮。我已经编写了这段代码,并想通过 fileName 或 _id 删除一个任务。
Server.js 中的代码
app.delete("/deleteTask/:id", function (req, res) {
console.log("TEST");
const task_id = req.params.id;
taskCollection.deleteOne({ "_id" : ObjectId(task_id) }, function (err, success) {
console.log(success);
res.redirect("/");
});
res.send(); });
View.js 中的代码
$.ajax({
"url": "/featured-tasks",
"method": "GET"
}).done(function(tasks) {
tasks.forEach(function(task) {
const taskCard = "<tr class='task-card'>" +
"<td class='title'>" + task.title + "</td>" + "<td class='description'>" + task.description + "</td>" +
"<td class='courses'>" + task.courses + "</td>" +
"<td class='task'>" + "<a href='/view?task=" + task.fileName + "'>" + task.fileName +"</a>" + "</td>" +
"<td>" + "<button id='taskdelete' class='btn-delete' data-target='/deleteTask/' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
"</tr>";
$(".task-gallery").append(taskCard);
})
});
$('#taskdelete').on('click', function() {
const taskFileName = $(this).attr('task.fileName');
console.log("test");
$.ajax({
method: "DELETE",
url: "/deleteTask/" + taskFileName,
success: function(result) { {
location.reload();
}
}
})
});
表格的 HTML 片段:
<table class="task-gallery">
<tbody>
<tr class="tableHeader">
<th>Title</th>
<th>Description</th>
<th>Courses</th>
<th>PDF-file</th>
<th></th>
</tr>
<script src="home/home.js"></script>
<tr class="task-card">
<td class="title">Larman</td>
<td class="description">lektier</td>
<td class="courses">info</td>
<td class="task"><a href="/view?task=file.pdf">file.pdf</a></td>
<td><button class="btn-delete" data-filename="file.pdf" data-method="DELETE" data-disabled="true">Delete</button></td>
</tr>
</tbody>
</table>
【问题讨论】:
-
你确定这是正确的吗?
const taskFileName = $(this).attr('task.fileName');您的任务删除按钮似乎没有任何名为“task.fileName”的属性。 -
我不确定它是否正确。但不太清楚我做错了什么或为什么它不起作用。
-
你能在
const taskFileName下面做一个console.log(taskFileName)吗?它应该返回您要删除的任务文档的 ID。 -
终端什么也没有出来,我的console.log("test")也没有,不知道哪里出错了,以前是
-
当你点击删除按钮时它应该会显示出来。
标签: javascript html node.js ajax mongodb