1216

91 阅读1分钟

后端查询代码:

 $.ajax({
          url: "http://localhost:3000/tag",
          type: "GET",
          success: function (res) {
              if (res.status === 200) {
                  const data = res.data;
                  let html = "";
                  for (let index = 0; index < data.length; index++) {
                      const element = data[index];
                      html += `
                      <div class="alert alert-info pull-left">
     <button type="button" data-id="${element._id}" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <strong>${element.text}</strong> </div>
                `;
                  }
                  $(".col-xs-12").html(html);
              }
              console.log(res);
          },
          error: function (err) {
              console.log(err);
          }
      })

删除代码:

     $(".col-xs-12").on("click",".close",function(){
    var id  =$(this).attr("data-id");               
    $.ajax({
      url:"http://localhost:3000/tag",
      type:"DELETE",
      data:{_id:id},
      success:function(res){
        if(res.status===200){
          console.log("删除成功")
        }
      }
    })    
    // console.log($(this));

       })

data:{text:$("#text").val() },这个是输入框内容