删除父div的父级

时间:2013-11-25 18:14:33

标签: php jquery html

我有一个表单,如果用户点击删除,我需要删除整行。

            <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();

1 个答案:

答案 0 :(得分: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>