2013-11-25 53 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> 

當用戶點擊刪除按鈕,我想刪除整個行(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> 

您的意思是「我在哪裏放置了我的javascript代碼,我該如何將它附加到按鈕上?」

<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