-
글쓴이글
-
2022년 1월 26일 03:06 #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>
2022년 1월 26일 21:08 #25853
하신영참가자위처럼 삭제를 누르면 ajax 성공하고, done이 실행돼 console에 메세지가 뜹니다. 물론 db값도 잘 삭제되구요. 그런데 (e)=>{} 를 function(e){}로 고쳐봐도
$(this).parent('tr').fadeOut()이 작동하지 않습니다.
addEventListner 대신 제이쿼리 click문을 사용해도 그렇습니다. 어떻게 하면 고칠 수 있을까요
<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>
<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>
아래는 제 리스트 코드입니다.
<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>
2022년 1월 26일 23:07 #25867
codingapple키 마스터$(this).parent('tr').html() 이런걸 콘솔창에 출력해봅시다
아니면 $(this) 대신 $(e.target)을 써봅시다
-
글쓴이글
- 답변은 로그인 후 가능합니다.