简体   繁体   English

删除包含表的表行

[英]Remove table row that contains a table

I have html that displays a table similar to: 我有显示类似表的html:

<table>
<tr>
    <th>col1</th>
    <th>col2</ht>
</tr>
<tr>
    <td>0001</td>
    <td>Test</td>
</tr>
<tr>
    <td colspan="2" id="detailsTable">
        <table>
            <tr>
                <th>one</th>
                <th>two</th>
            </tr>
            <tr>
                <td>xxxxxx</td>
                <td>xxxxxxx</td>
            </tr>               
        </table>
    </td>
</tr>

There is a column of expand and contract buttons on the outer table so that the nested table is only shown when the user clicks to expand. 外部表上有一列展开和收缩按钮,因此仅当用户单击以展开时才显示嵌套表。

The expansion works and the table gets displayed. 扩展工作,表格显示出来。 However when when I try and remove the row from the outer table that contains the child table it doesn't work. 但是,当我尝试从包含子表的外部表中删除该行时,它行不通。

I had code like: 我有这样的代码:

var rowIndex = $(this).parent().parent().prevAll().length;
$("table[id$=gvParentAccounts] tr").eq(rowIndex + 1).remove();

If the row only contains text it works as I'd like and removes the row, however if like in this case the row contains a table it is unable to remove the row as required. 如果该行仅包含文本,则它将按我的意愿工作并删除该行,但是,如果在这种情况下,该行包含一个表,则无法根据需要删除该行。

I'm using ASP.Net and jQuery for this. 我为此使用ASP.Net和jQuery。

Thanks Alan. 谢谢艾伦。

How about: 怎么样:

$(this).parent().parent().remove();

I'm not sure if this is exactly what you have, but here's a JSFiddle demonstrating that it works: 我不确定这是否正是您所拥有的,但是这是一个JSFiddle演示了它的工作原理:

http://jsfiddle.net/9TQG9/1/ http://jsfiddle.net/9TQG9/1/

EDIT: Actually this: 编辑:其实这:

$(this).parents("tr").eq(0).remove();

would be much nicer and more reliable. 会更好,更可靠。 See here: 看这里:

http://jsfiddle.net/9TQG9/2/ http://jsfiddle.net/9TQG9/2/

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

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