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