javascript - 在 Node.js、Mongodb、Ajax、HTML 中删除时出现问题

标签 javascript html node.js ajax mongodb

我无法使用删除按钮。我有一个 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>&nbsp;</th>
    </tr>
  </tbody>
</table>

关于javascript - 在 Node.js、Mongodb、Ajax、HTML 中删除时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53610586/

相关文章:

JavaScript 变量作为 HTML 链接文本

javascript - 如何检查对象中键的值并在 if 语句中使用它? JavaScript

node.js - 使用 Node.js 集群模块在不同用户下 fork 一个工作线程?

node.js实时游戏

node.js - 在新的流程 Node 中执行功能

javascript - 如果文本框位于特定页面上,则调用文本框上的方法

javascript - 当弹出窗口在前面时如何突破 iFrame?

带实心边框的 CSS 梯形按钮

html - 什么/在哪里生成这个 CSS 代码到我的 Wordpress 页面

javascript - 部分网站在较小的屏幕上消失