• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Node.js, Express 게시판 ajax 삭제시 $(this).parent('tr').fadeOut()

ajax 삭제시 $(this).parent('tr').fadeOut()

5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 글쓴이
  • #25747

    하신영
    참가자

    전 li 태그가 아니라 table의 tr에 각 데이터가 나오도록 했는데요. ajax로 해당 tr 데이터 삭제시 성공하면 $(this).parent('tr').fadeOut() 하고 싶은데 안됩니다.

    분명 삭제는 성공해서 done 함수가 실행되는데 왜 fadeOut만 안먹힐까요.

    list.ejs 코드
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script>
    let delBtn = document.querySelector('.delBtn');

    delBtn.addEventListener("click",(e)=>{
    $.ajax({
    method : 'DELETE',
    url : '/delete',
    data : { _id: e.target.dataset.id }
    }).done((result) => {
    console.log(result.msg)
    $(this).parent('tr').fadeOut();

    }).fail(function(xhr, textStatus, errorThrown ){
    console.log(xhr, textStatus, errorThrown);
    })
    });

    </script>
     

    #25760

    codingapple
    키 마스터

    이벤트리스너 안에선 화살표함수를 쓰면 안됩니다 function으로 바꿉시다 

    #25853

    하신영
    참가자

    list 웹페이지

    위처럼 삭제를 누르면 ajax 성공하고, done이 실행돼 console에 메세지가 뜹니다. 물론 db값도 잘 삭제되구요. 그런데 (e)=>{} 를 function(e){}로 고쳐봐도

    $(this).parent('tr').fadeOut()이 작동하지 않습니다. 

    addEventListner 대신 제이쿼리 click문을 사용해도 그렇습니다.  어떻게 하면 고칠 수 있을까요

    삭제 ajax addEvent
    <blockquote>delBtn.addEventListener("click",function(e){
    $.ajax({
    method : 'DELETE',
    url : '/delete',
    data : { _id: e.target.dataset.id }
    }).done(function(result){
    console.log(result.msg)
    console.log($(this).parent('tr').fadeOut())
    $(this).parent('tr').fadeOut();
    }).fail(function(xhr, textStatus, errorThrown ){
    console.log(xhr, textStatus, errorThrown);
    })
    });</blockquote>
    삭제 ajax jQuery
    <blockquote>$('.delBtn').click(function(e){
    $.ajax({
    method : 'DELETE',
    url : '/delete',
    data : { _id: e.target.dataset.id }
    }).done(function(result){
    console.log(result.msg)
    $(this).parent('tr').fadeOut();

    }).fail(function(xhr, textStatus, errorThrown ){
    console.log(xhr, textStatus, errorThrown);
    })

    })

     </blockquote>
    아래는 제 리스트 코드입니다.

    list

     
    <blockquote><% for(var i= 0; i< data.length; i++){ %>
    <tr>
    <th scope="row"><%= data[i].name %></th>
    <td><%= data[i].pw %></td>
    <td><%= data[i].email %></td>
    <td>
    <button type="button" class="delBtn" data-id="<%= data[i]._id %>">삭제</button>
    </td>
    </tr>
    <% }%></blockquote>
     

    #25867

    codingapple
    키 마스터

    $(this).parent('tr').html() 이런걸 콘솔창에 출력해봅시다

    아니면 $(this) 대신 $(e.target)을 써봅시다 

    #83604

    신미화
    참가자
    console.log($(this).parent('tr').html()); 찍었더니 undefined나와서 
    parent('tr') ----> parents('tr')로 바꿨더니 잘 fadeOut이 되네요
    
    
5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠