简体   繁体   English

按id从表中删除一行

[英]Delete a row from a table by id

I have a little problem.我有一个小问题。 I have some dynamically created tables and each row has an id.我有一些动态创建的表,每一行都有一个 id。 I want to delete the row with the id "x".我想删除 ID 为“x”的行。

I tried the usual method (removeChild) but it doesn't work for tables apparently.我尝试了通常的方法 (removeChild),但它显​​然不适用于表格。

function deleteRow(tableid, rowid)  
{   
      document.getElementById(tableid).removeChild(document.getElementById(rowid));  
}   

The error I get is: Node was not found" code: "8我得到的错误是:找不到节点”代码:“8

I also tried this:我也试过这个:

function deleteRow(tbodyid, rowid)   
{  
      document.getElementById(tbodyid).removeChild(document.getElementById(rowid));   
}   

and got the same error.并得到同样的错误。

I can't use the deleteRow() method because that one needs the index of the row and I prefer not to search for the id mark the index then delete (even though if I don't find other solutions...).我不能使用deleteRow()方法,因为那个方法需要行的索引,我不想搜索 id 标记索引然后删除(即使我没有找到其他解决方案......)。

How about:怎么样:

function deleteRow(rowid)  
{   
    var row = document.getElementById(rowid);
    row.parentNode.removeChild(row);
}

And, if that fails, this should really work:而且,如果失败了,这应该真的有效:

function deleteRow(rowid)  
{   
    var row = document.getElementById(rowid);
    var table = row.parentNode;
    while ( table && table.tagName != 'TABLE' )
        table = table.parentNode;
    if ( !table )
        return;
    table.deleteRow(row.rowIndex);
}

From this post , try this javascript:从这篇文章,试试这个javascript:

function removeRow(id) {
  var tr = document.getElementById(id);
  if (tr) {
    if (tr.nodeName == 'TR') {
      var tbl = tr; // Look up the hierarchy for TABLE
      while (tbl != document && tbl.nodeName != 'TABLE') {
        tbl = tbl.parentNode;
      }

      if (tbl && tbl.nodeName == 'TABLE') {
        while (tr.hasChildNodes()) {
          tr.removeChild( tr.lastChild );
        }
      tr.parentNode.removeChild( tr );
      }
    } else {
      alert( 'Specified document element is not a TR. id=' + id );
    }
  } else {
    alert( 'Specified document element is not found. id=' + id );
  }
}

I tried this javascript in a test page and it worked for me in Firefox.我在测试页面中尝试了这个 javascript,它在 Firefox 中对我有用。

to Vilx-:到 Vilx-:

var table = row.parentNode;
while ( table && table.tagName != 'TABLE' )
    table = table.parentNode;

and what if row.parentNode is TBODY ?如果row.parentNodeTBODY呢?

You should check it out first, and after that do while by .tBodies , probably你应该先检查一下,然后通过.tBodieswhile .tBodies ,可能

那么尝试不删除而是隐藏该行呢?

Something quick and dirty:一些快速而肮脏的事情:

<script type='text/javascript'>
function del_tr(remtr)  
{   
    while((remtr.nodeName.toLowerCase())!='tr')
        remtr = remtr.parentNode;

    remtr.parentNode.removeChild(remtr);
}
function del_id(id)  
{   
        del_tr(document.getElementById(id));
}
</script>

if you place如果你放置

<a href='' onclick='del_tr(this);return false;'>x</a>

anywhere within the row you want to delete, than its even working without any ids您要删除的行中的任何位置,甚至在没有任何 id 的情况下工作

The parent of the row is not the object you think, this is what I understand from the error.该行的父级不是您认为的对象,这是我从错误中了解到的。
Try detecting the parent of the row first, then you can be sure what to write into getElementById part of the parent.首先尝试检测该行的父级,然后您可以确定将什么写入父级的getElementById部分。

*<tr><a href="javascript:void(0);" class="remove">X</a></tr>*
<script type='text/javascript'>
  $("table").on('click', '.remove', function () {
       $(this).parent().parent('tr').remove();
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM