jQuery - 通过点击<td>删除表格行<tr>

17

我正在制作一张表格,可以添加额外的行。当您添加一行时,可以保存或取消,通过点击取消将删除该行。它适用于一行,但当我创建大约六个时,如果点击取消,则所选行不会被删除,而是最后一行将被删除。这是我的代码,有人知道我做错了什么吗?

<head>
  <script src="../jquery.js" type="text/javascript"></script>

  <script type="text/javascript">
  $(document).ready(function() {
  $(".edit").click(function() {
    var id = $(this).attr("id");
    alert("edit "+id);
  });
  $(".delete").click(function() {
    var id = $(this).attr("id");
    alert("delete "+id);
  });
  $("#newbutton").click(function() {
  var randomnumber=Math.floor(Math.random()*100);
    $("tr:last").after("<tr id="+randomnumber+"><td><form><input style='width: 80%'></form></td><td class=ok>OK</td><td id="+randomnumber+" class=cancel>Cancel</td></tr>").ready(function() {
      $("tr td .cancel").click(function() {
        $(this).remove();
      });
      $(".ok").click(function() {
        alert("OK!");
      });
    });
  })
}); 
  </script>
</head>
<table border=1 id=table>
<tr><th>Name</th></tr>
<tr><td>Bombai</td><td id=1 class=edit>edit</td><td id=1 class=delete>delete</td></tr> 
<tr><td>London</td><td id=2 class=edit>edit</td><td id=2 class=delete>delete</td></tr> 
<tr><td>Rom</td><td id=3 class=edit>edit</td><td id=3 class=delete>delete</td></tr> 
</table><label id=newbutton>New Place</label>

抱歉,但是迄今为止,两个答案都没有解决我的问题 :( - elhombre
抱歉,我的问题完全得到了解决,多亏了duckyflip的答案! - elhombre
7个回答

20

由于你正在动态地向DOM添加行,我建议您使用 live 函数:

$("tr td .cancel").live("click", function(){
  $(this).parent("tr:first").remove()
})

非常感谢你,duckyflip,你帮了我很多! - elhombre
3
live() 方法在 jQuery 1.7 版本中被弃用,在 1.9 版本中被移除。请改用 on() 方法。引自:http://www.w3schools.com/jquery/event_live.asp - Sinan Samet
这是最新的代码: $( document ).on( "click", ".remove", function(){ $(this).parent("tr:first").remove() }); - Lakshman Pilaka

12
<td><a class="delete" onclick ="delete_user($(this))">Delete</a></td>

在JavaScript中

    function delete_user(row)
    {
        row.closest('tr').remove();


    }

7

您可以尝试像这样做:

查看此 演示 js Fiddle

HTML

<table border=2>
    <tr>
        <td>jQuery</td>
        <td>mootools</td>
    </tr>
    <tr>
        <td>Dojo</td>
        <td>FUEL</td>
    </tr>
    <tr>
        <td>Raphael</td>
        <td>Rico</td>
    </tr>
    <tr>
        <td>SproutCore</td>
        <td>Lively Kernel</td>
    </tr>
</table>

jQuery :

$(function() {
   $('tr')
       .find('td')
       .append('<input type="button" value="Delete" class="del"/>')
       .parent()//traversing to 'tr' Element
       .append('<td><input type="button" value="Delete row" class="delrow" /></td>');

    $('.del').click(function() {
       $(this).parent().remove(); //Deleting TD element
    });

    $('.delrow').click(function(){
       $(this).parent().parent().remove(); //Deleting the Row (tr) Element
    });
});

0
尝试以下代码:
$(this).closest("tr").remove();

0

您没有将删除操作限定在添加的行的上下文中。请尝试:

$(this).find("tr td .cancel").click(...)

需要对编辑按钮进行相同的处理。


0

脚本

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script>
    $(document).ready(function () {
        var markup = "<tr><td><input type='checkbox' name='record'></td><td></td><td></td></tr>";
        $("#Add").click(function () {
            $("#tbl1").append(markup);

        });
        $("#remove").click(function () {
            $("table tbody").find('input[name="record"]').each(function () {
                if ($(this).is(":checked")) {
                    $(this).parents("tr").remove();
                }
            });
        });
    }); </script>

-1

正文

添加行

<table border="5px" cellspacing="3px" cellpadding="10px" id="tbl1">
    <thead>
        <tr>
        <td>
            Select
        </td>
        <td>
            column 2
        </td>
        <td>
            column 3
        </td>

    </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="record"></td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>
<br />
<button id="remove">Delete Row</button>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接