2013-11-25 3 views
0

사용자가 제거를 클릭하면 전체 행을 제거해야하는 양식이 있습니다. 사용자가 클릭 버튼을 제거 할 때부모 div의 부모 제거

  <div id='a_<?php echo $a;?>'> 
        <div class='leftDesc'>Code</div> 
        <div class='inputText'><input type='text' name='code[]' value='<?php echo $code;?>' id='code[]'></div> 
        <div class='centerDesc'>Description</div> 
        <div class='inputText'><input type='text' name='desc[]' value='<?php echo $desc;?>' size='32' maxlength='32' id='desc[]'></div> 
        <div class='centerDesc'><input type='button' class='remDisp' value='Remove Code' id='removeCode_<?php echo $a;?>'></div> 
      </div> 

, 난 모든 행을 제거 할 (사업부의 A_의 $ a를)

내가 어떻게 기본적으로 쓰기 수행

$(this).parent().parent().remove(); 
+5

작동하지 않습니까? – Krishna

+0

해당 버튼의 전체 핸들러 게시 – tymeJV

+1

권자. 그 작품. parent.parent는 어리석은 방법으로 보였습니다. 부모님의 신분을 알고 계시다면 – bart2puck

답변

0

당신이 찾고있는 경우 요소 위치에 의존하지 않는 대체 접근법은 다음과 같이 데이터 속성 또는 클래스로 연결합니다. Live demo here (click).

<div id="bar"></div> 
<button class="remDisp" data-foo="bar">Some Button</button> 

<script> 
    $(document).ready(function() { 

    var buttons = $('.remDisp'); //get a reference to your buttons 
    buttons.click(function() { //add the click function to the buttons 
     var foo = $(this).attr('data-foo'); //get "foo" attr (value of "bar" in this case) 
     $('#'+foo).remove(); //find the element with id of foo's value (bar) 
    }); 

    }); 
<script> 

"내 자바 스크립트 코드는 어디에 두어야하나요? 어떻게 단추에 부착합니까?"

<script> 
    $(document).ready(function() { 

    var buttons = $('.remDisp'); //get a reference to your buttons 
    buttons.click(function() { //add the click function to the buttons 
     $(this).parent().parent().remove(); //remove the grandparent of the clicked button. 
    }); 

    }); 
<script> 
+0

나는 응답에 감사한다. 대답은 사용자의 어리 석음이었다. 나중에 닥터. – bart2puck

관련 문제