简体   繁体   English

为什么 onClick 的删除按钮 ajax 调用在按钮附加到 div 元素后不起作用?

[英]Why onClick of delete button ajax call is not working after the button is being appended into the div element?

On load page I am appending the comments via AJAX call and all the comments have been successfully posted.在加载页面上,我通过AJAX调用附加评论,所有评论都已成功发布。 All the comments have their respective delete buttons with the common class and unique ids.所有评论都有各自的删除按钮,带有通用 class 和唯一 ID。 So Onclick of delete button I want to delete the respective comment but however its access is not coming into the AJAX call.所以删除按钮的Onclick我想删除相应的评论,但是它的访问权限没有进入AJAX调用。 I have tried with alert as well but its not working I don't know why.我也尝试过 alert 但它不起作用我不知道为什么。 What I am doing wrong?我做错了什么?

 <script>
    $(document).ready(function () {
    
    var AddtasksId = $('#submitBtn4comment').attr('data-taskId');
    
    $.ajax({
      type: "GET",
      url: "/getComments",
      dataType: "json",
      data: {
        AddtasksId: AddtasksId
      },
      contentType: "application/json",
      success: function(response){
    $.each(response, function(key,comment) {
    appendToDom(comment);
         });
    }
    });
    
    });
    
    function appendToDom(data) {
        let lTag = document.createElement('li')
        lTag.classList.add('comment', 'mb-3')
    
        let markup = `
        <div class="card border-light mb-3">
            <div class="card-body">
                <h5>${data.username}</h5>
                <p style="background-color:#E8E8E8">${data.comment}</p>
                <div><h6>⏲️ ${data.commentDate}<span class="pull-right"></span></h6></div>
                <a href="/deleteComment" id="${data._id}" class="deleteComments">Delete Comment</a>
                <div id="line"><hr  style="" /></div>
            </div>
        </div>
        `
        lTag.innerHTML = markup
    
        $(".comment__box").prepend(lTag);
    }
    
    </script>

html div tags where it is appending: html 追加的 div 标签:

<div class="row">
    <div class="col-md-12">
        <!-- <div class="typing text-success"></div> -->
        <ul class="comment__box commonText">
        </ul>


</div>
</div>

This is AJAX call for deleting the comment: but on clicking the <a> button the access is not coming means alert doesn't popup.这是删除评论的AJAX调用:但是在单击<a>按钮时访问未到来意味着不会弹出alert

$(".deleteComments").on("click", function() {

  alert("First alert");

    var id = $(this).prop("id");
    var AddtasksId = $('#submitBtn4comment').attr('data-taskId');

  $.ajax({
    type: "GET",
    url: "/deleteComment",
    dataType: "json",
    data: {
      AddtasksId: AddtasksId,
      id:id
    },
    contentType: "application/json",
    success: function(response){
    if(response.status == 200)
    window.location.reload();
  }
  });
})

The click event is not triggered because the element does not exist when the event is defined.没有触发点击事件,因为定义事件时元素不存在。

you can solve that this way:你可以这样解决:

$(document).on("click", ".deleteComments", function() {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM