2010-08-03 2 views
2

웹 응용 프로그램에서 jQuery를 사용하여 HTML 테이블에서 행을 동적으로 추가/삭제하고있었습니다.jQuery를 사용하여 행을 삭제하는 동안 개별 td 제거

개인의 TD 요소 (전체 행이 아닌)를 삭제해야한다는 요구 사항이있었습니다.

$(function() { 

    /* adding new row to existing table */ 
    // HTML template of a row 
    $("#contactInfo").each(function() { 
     $("button.addCommentRow", this).live('click', function() { 

      var curr_row = $(this).closest('tr'); 
      var html = '<tr><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> ' + col_ninth + '</td> <td><button type="button" class="addCommentRow">+</button> <button type="button" class="deleteCommentRow">-</button> </td> </tr>'; 

      var newRow = $(html).insertAfter(curr_row); // add after the last 
      newRow.find('.addCommentRow, .deleteCommentRow').button(); 
      newRow.find('.deleteCommentRow').button().click(function() { 

       newRow.find("td:nth-child(9)").remove(); 
       newRow.find("td:nth-child(10)").remove(); 

      }); 
     }); 
    }); 
}); 

을 그래서, 9 내 행에서 10 번째 TD 요소를 제거하고 싶었 삭제에 다음과 같이

내 jQuery 코드입니다.

위 코드 (삭제 부분 만)가 작동하지 않는 것 같습니다.
아이디어를 얻으려면 제안을 받아야합니다.

고마워,
피셔.

+3

테이블 행이 올바르게 렌더링되기 위해서는 일치 또는 열 구분 된 셀이 필요하므로 레이아웃을 혼란스럽게 만들 것입니다. 대신 이러한 셀의 내용을 삭제할 수 있습니까? – Paddy

+0

게시물을 보내주세요. –

+0

@Paddy : 셀 내용도 삭제할 수 있습니다. 어떻게하면됩니까? – Pritish

답변

4

이 뭔가를 시도 할 수 있으며, 데모을 시도 : 당신은 아마 이런 식으로 뭔가를 원하는

$(function() { 
    $('tr') 
     .find('td') 
     .append('<input type="button" value="Delete" class="del"/>') 
     .parent()//traversing to 'tr' Element 
     .append('<td><input type="button" value="Delete row" class="delrow" /></td>'); 

    $('.del').click(function() { 
     $(this).parent().remove(); //Deleting TD element 
    }); 

    $('.delrow').click(function(){ 
     $(this).parent().parent().remove(); //Deleting the Row (tr) Element 
    }); 
}); 
1

나는 Irwin에 동의합니다. http://jsfiddle.net/HXB4Z/

HTML :

<table border=2> 
    <tr> 
     <td>jQuery</td> 
     <td>mootools</td> 
    </tr> 
    <tr> 
     <td>Dojo</td> 
     <td>FUEL</td> 
    </tr> 
    <tr> 
     <td>Raphael</td> 
     <td>Rico</td> 
    </tr> 
    <tr> 
     <td>SproutCore</td> 
     <td>Lively Kernel</td> 
    </tr> 
</table> 

자바 스크립트 :

newRow.find("td:nth-child(9)").html(''); 
newRow.find("td:nth-child(10)").html(''); 
+1

메모리가'.html ('')' –

+0

으로 누출 되었기 때문에 여기서는'.empty()'를 사용하는 것이 좋을 것입니다. 자바 스크립트가 가비지 콜렉션을 돌보고 싶다고 생각합니다. 또는 나는 너무 낙관적입니까? 그러나 나는 empty()가이 경우 더 우아하다는 것에 동의한다. –

+0

@Nick : 하나의 작은 일을 제외하고 .empty()가 작동하는 것 같습니다. 나는 또한 내가 마지막으로 haviny haviny 어떤 셀 내용이 발생한다면, 그 후에도 tr 삭제 삭제할 10 번째 요소가 있다면, 물론 확인하고 싶습니다. 이 말을하는 이유는, 삭제 된 후에 행 테두리 뒤에서 떠나는 개별 요소의 비어있는 .empty()와 함께있는 것입니다. – Pritish

관련 문제