5

我了解如何获取$('#tableid tr').attr('data-id')使用 HTML5 数据属性的表对象的属性。但是,当我尝试根据 data-id 属性删除行时,它似乎不起作用。

当我做这样的事情时:

var theRowId = $('#tableid tr').attr('data-id');
$('#tableid tr#'+theRowId).remove();

它没有用。Html 5 数据属性应该像任何其他属性一样处理,对吗?

4

3 回答 3

9

您需要传递要从中获取数据属性的 tr的索引

$('#tableid tr:eq(0)'); // 表格的第一行

$('#tableid tr:eq(1)'); // 表格第二行

因为表中可能有多行

var theRowId = $('#tableid tr:eq(1)').attr('data-id'); // Get the Second Row id
$('#tableid tr#'+theRowId).remove();  // Remove the row with id

或者,如果您知道行的 ID .. 只需执行此操作

$('#tableid tr[data-id="'+theRowId+'"]').remove();
于 2012-11-02T18:40:33.627 回答
5

您需要更改为 data-id 属性选择的方式。尝试选择这样的行:

$('#tableid tr[data-id="'+theRowId+'"]').remove();

这将为具有匹配的 data-id 属性的 tr 选择。

于 2012-11-02T18:41:50.127 回答
2

data-id 不像 id

你需要这样做:

  $('#tableid tr[data-id='+ theRowId +']').remove()
于 2012-11-02T18:41:28.473 回答