문제

나는 하나의 줄과 동적으로 추가되는 여러 trs가있는 HTML 테이블이 있습니다. 각 TR에는 마지막 열에 버튼이 있습니다.

버튼을 클릭하여 가장 가까운 TR이 테이블에서 제거되도록 무엇이 필요합니까? 나는 사용을 시도했다 $(this).closest.remove() 그러나 이것은 작동하지 않았으므로 여기에 ID 또는 다른 것을 추가해야한다고 가정합니다.

기본 예제 테이블은 다음과 같습니다.

<table class='tableClass'>
    <tbody>
        <th>
            <td>Text</td><td>Text</td><td>Text</td>
        </th>
        <tr>
            <td>Text</td><td>Text</td><td><button type='button' class='btnClass'>Delete</button></td>
        </tr>
        <tr>
            <td>Text</td><td>Text</td><td><button type='button' class='btnClass'>Delete</button></td>
        </tr>
        <tr>
            <td>Text</td><td>Text</td><td><button type='button' class='btnClass'>Delete</button></td>
        </tr>
    </tbody>
</table>
도움이 되었습니까?

해결책

당신은 주어야합니다 closest 선택기를 기능하십시오. 이 시도:

$('.btnClass').click(function() {
    $(this).closest('tr').remove();
});

또한 HTML은 유효하지 않습니다 th 아이가되어야합니다 tr:

<table class='tableClass'>
    <tbody>
        <tr>
            <th>Text</th>
            <th>Text</th>
            <th>Text</th>
        </tr>
        <tr>
            <td>Text</td>
            <td>Text</td>
            <td>
                <button type='button' class='btnClass'>Delete</button>
            </td>
        </tr>
        <tr>
            <td>Text</td>
            <td>Text</td>
            <td>
                <button type='button' class='btnClass'>Delete</button>
            </td>
        </tr>
        <tr>
            <td>Text</td>
            <td>Text</td>
            <td>
                <button type='button' class='btnClass'>Delete</button>
            </td>
        </tr>
    </tbody>
</table>

예제 바이올린

다른 팁

당신은 이것을 사용하여 할 수 있습니다 .closest( selector ) 적절하게 좋아요 :

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

실제로 코드에서 :

$(this).closest.remove()
            ___^___

당신은 개구부와 폐쇄 괄호가 모두 누락되었습니다 () 그리고 선택기 tr.

http://jsfiddle.net/cpvh9/

$(".btnClass").click(function(){
        $(this).parents('tr').remove();
    });

HTML 코드에서 부모 TR을 제거하고 싶은 것처럼 보입니다.

$(".btnClass").click(function(){
    $(this).parents("tr:first").remove();
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top