2016-11-02 4 views
-2

, 내가 표 1 이 TR을 추가하고 표 1에 추가하기 전에 테이블 내부의 TR에서 TD를 제거하는 방법, 난이 특정 TD는 제거 버튼의 클릭에

<td><a data-videoid="6" class="btn btn-delete removevideo btn-danger ui-link">Remove</a></td> 
을 제거해야

이이 내 바이올린 내 코드

$(document).on("click", ".removevideo", function(event) 
{ 


      if (confirm("Are You Sure to Remove This Video From This PAC?") == true) 
      { 
       var video_id = $(this).data('videoid'); 
       $(this).closest("tr").removeClass('existingvideos'); 
       var html = $('#table1 tr[video-id="'+video_id+'"]').prop('outerHTML'); 
       $('#table1 tr[video-id="'+video_id+'"]').remove(); 
        $("#table2 tbody").append(html); 

      }   
      event.stopImmediatePropagation(); 
      event.preventDefault(); 
      return false; 



}); 

입니다

http://jsfiddle.net/cod7ceho/271/

+1

http://stackoverflow.com/questions/40374508/is-it-possible-to-remove-a-table-row-based-on-the-data-attribute – Satpal

답변

0

시험해보기 :

$(this).closest("tr").remove(); 

제거가 클릭 된 가장 가까운 tr이 제거됩니다.

확인 Updated Fiddle

2

즉각적인 문제를 해결하기 위해 당신은 $(this).closest('td').remove()를 사용할 수 있습니다. 그러나 videoId으로 선택하는 대신 DOM 통과를 사용하여 코드를 크게 단순화 할 수도 있습니다. 이 시도 :

$(document).on("click", ".removevideo", function(e) { 
    e.preventDefault(); 
    var $a = $(this); 
    var $tr = $a.closest('tr'); 

    if (confirm("Are You Sure to Remove This Video From This PAC?")) { 
     $tr.removeClass('existingvideos').remove(); 
     $a.closest('td').remove(); 
     $("#table2 tbody").append($tr); 
    } 
}); 

Updated fiddle

+0

의 질문에 따라 배우지 마십시오. 나는 $ (this) .closest ("tr")로 관리하고 있었다. find ("td : last"). remove(); , 그러나 당신의 코드는 더 좋아진다. – Pawan

0

사용

var html = $('#table1 tr[video-id="'+video_id+'"] td:not(:last-child)').prop('outerHTML'); 

instead.it가 추가되기 전에 마지막 열을 제거합니다.

관련 문제