【问题标题】:removing parent of a parent div删除父 div 的父级
【发布时间】:2013-11-26 02:34:18
【问题描述】:

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

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

【问题讨论】:

  • 发布该按钮的整个处理程序
  • 哈哈。这样可行。 parent.parent 似乎写得很傻。请原谅这么荒谬的问题.....
  • 如果你知道父母的身份证$(this).parents('#parent id').remove();
  • 这个问题似乎是题外话,因为 OP 在他的问题中回答了他自己的问题。
  • @AlexandreDiacov 如果你知道父母的身份证,那么只需将其删除:$('#parent_id').remove();

标签: php jquery html


【解决方案1】:

如果您正在寻找一种不依赖于元素位置的替代方法,请通过数据属性或类将它们关联起来,如下所示: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>

【讨论】:

  • 感谢您的回复。答案是用户愚蠢。生病了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2011-03-22
  • 2023-04-06
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
相关资源
最近更新 更多