jquery 테이블 rowspan tr 삭제 질문입니다.

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
jquery 테이블 rowspan tr 삭제 질문입니다.

QA

jquery 테이블 rowspan tr 삭제 질문입니다.

본문

cffd88cbd67e3f24061a480509d7f2de_1489124845_8015.png

삭제 버튼을 눌렀을때  채크박스를 선택해서 빨간 박스안의 tr을 remove()하고 싶습니다.

 

 

빨간색 박스를 선택하려면 어떻게 해야 하나요?

 


<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
<button id="delete">삭제</button>
 
  <table>
    <tr>
      <th>lesson</th>
      <th>class</th>
      <th>absent</th>
      <th>substitute</th>
    </tr>
    <tr>
      <td rowspan="2"><input type="checkbox" id="cbx" name="cbx"/></td>
      <td>1b</td>
      <td>John</td>
      <td>Max</td>
    </tr>
    <tr>
      <td>3a</td>
      <td>Bev</td>
      <td>Abbi</td>
    </tr>
    <tr>
      <td rowspan="2"><input type="checkbox" id="cbx" name="cbx"/></td>
      <td>1b</td>
      <td>John</td>
      <td>Max</td>
    </tr>
    <tr>
      <td>3a</td>
      <td>Bev</td>
      <td>Abbi</td>
    </tr>
  </table>
</body>
</html>

이 질문에 댓글 쓰기 :

답변 4


<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
    $(function(){
        $("#delete").click(function(){
            $("input[name=cbx]:checked").each(function(){
                 $(this).parent().parent().next().remove();
                $(this).parent().parent().remove();
            });
            
        })
    });    
 
</script> 
<button id="delete">삭제</button>
 
  <table>
    <tr>
      <th>lesson</th>
      <th>class</th>
      <th>absent</th>
      <th>substitute</th>
    </tr>
    <tr>
      <td rowspan="2"><input type="checkbox" name="cbx"/></td>
      <td>1b</td>
      <td>John</td>
      <td>Max</td>
    </tr>
    <tr>
      <td>3a</td>
      <td>Bev</td>
      <td>Abbi</td>
    </tr>
    <tr>
      <td rowspan="2"><input type="checkbox" name="cbx"/></td>
      <td>1b</td>
      <td>John</td>
      <td>Max</td>
    </tr>
    <tr>
      <td>3a</td>
      <td>Bev</td>
      <td>Abbi</td>
    </tr>
  </table>
</body>
</html>

</script> 

   

 

id="cbx" 는 제거해주세요. ID 는 중복하시면 안됩니다.


$(function() {
    $("#delete").on("click", function() {
        var chk = $("input[name='cbx']:checked").length;
        if(chk > 0) {
            $("input[name='cbx']:checked").each(function() {
                $(this).closest("tr").next().remove();
                $(this).closest("tr").remove();
            });
        } else {
            alert("삭제할 항목 없음");
        }
    });
});

http://nyaongii.dothome.co.kr/temp/wrid_172910.html

삭제하려는 tr끼리 같은 class를 주세요.. 물론 출력할때부터 고려해야겠죠... 

현재 반복문으로
$("input[name=cbx]").eq(i).is(":checked")으로 조건을 걸어서
참이면
$(this).parent("tr").remove(); 

이렇게 구현했는데
<td>두줄과 rowspan 된 td도 삭제되어야 하는데
한줄만 삭제되거나 그러네요

tr에 class를 주어서 고민해보겠습니다.

이것만 2일째 잡고 있는데 


 
<tr class="remove">
 
$('#테이블이름 tr .remove').click(function(){
   $(this).parent().next().remove();
   $(this).parent().remove();
})
 

 

이런식으로 하면 되나요

답변을 작성하시기 전에 로그인 해주세요.
전체 3

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT