我的删除按钮无法正常工作。我有一张有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>
前端代码中有一些内容需要更改:
- HTML元素的Id值必须是唯一的,这样您就不能使用您的点击处理程序
- 您需要将
task.fileName
值设置为删除按钮的属性 onclick
事件处理程序应该位于容器元素上
这里有一些更新的代码供您尝试:
$.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 class='btn-delete' data-filename='" + task.fileName + "' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
"</tr>";
$(".task-gallery").append(taskCard);
})
});
$('.task-gallery .btn-delete').on('click', function() {
const taskFileName = $(this).attr('data-filename');
console.log("test");
$.ajax({
method: "DELETE",
url: "/deleteTask/" + taskFileName,
success: function(result) {
location.reload();
}
})
});
在服务器端,您应该检查错误:
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) {
if (err){
console.log("failed");
throw err;
}
console.log(success);
res.redirect("/");
});
res.send();
});
我提供的代码是正确的,运行代码片段为自己验证:
var tasks = [
{
title: "A Course",
description: "Description of A",
courses: "Course A",
fileName: "file-A.pdf"
},
{
title: "B Course",
description: "Description of B",
courses: "Course B",
fileName: "file-B.pdf"
},
{
title: "C Course",
description: "Description of C",
courses: "Course C",
fileName: "file-C.pdf"
}
];
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 class='btn-delete' data-filename='" + task.fileName + "' data-method='DELETE' data-disabled='true'>" + "Delete" +"</button>" + "</td>" +
"</tr>";
$(".task-gallery").append(taskCard);
});
$('.task-gallery .btn-delete').on('click', function() {
const taskFileName = $(this).attr('data-filename');
// spit out the 'data-filename' value
window.alert('Delete ' + taskFileName);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<table class="task-gallery">
<tbody>
<tr class="tableHeader">
<th>Title</th>
<th>Description</th>
<th>Courses</th>
<th>PDF-file</th>
<th> </th>
</tr>
</tbody>
</table>