2014-02-23 6 views
2

기본적으로 동일한 .parent(). parent().와 같은 두 개의 다른 thinds를 추가합니다. remove() mehtod가 작동하는 첫 번째 테이블에서 두 번째로 그렇지 않습니다.동일한 제거 방법으로 별도의 추가가 작동하지 않음

아이디어가 있으십니까?

다음은 코드입니다. http://jsfiddle.net/rzmA4/

$(document).ready(function(){ 
     $('#adauga_model').on('click', function(){ 
      $('#modele_adaugate').append('<tr><td><input type="file" name="model[]"></td><td><button class="remove" type="button">Remove</button></td></tr>'); 
     }); 
     $('#adauga_design').on('click', function(){ 
      $('#designuri_adaugate').append('<tr><td><input type="file" name="design[]"></td><td><button class="remove" type="button">Remove2</button></td></tr>'); 
     }); 
     $("td").on('click', '.remove', function() { 
      $(this).parent().parent().remove(); 
      return false; 
     }); 
}); 

답변

3

전혀 처음에 일을하는 것이 재미있다,하지만 당신은 모든 TD 요소 때 문서 부하에 이벤트 리스너를 추가하고 있습니다. 그러나 이벤트 리스너가없는 새로운 이벤트를 작성 중입니다. 보너스로

$("body").on('click', 'td .remove', function() { 
     $(this).closest("tr").remove(); 
     return false; 
}); 

: 문제를 해결하기 위해 다음과 같이

나는 스크립트를 다시 썼다 closest("tr")parent().parent()하는 것보다 더 나은 사용. 후자를 수행하고 나중에 디자인을 변경하면 버그가 발생할 수 있습니다.