在.NET MVC项目中,搜索会生成一个包含标题和未知行数的表。这些行中的每一行都会隐藏一个嵌套表格,其中包含标题和未知数量的行。我想单击相应的行时嵌套表展开。使用jquery切换嵌套表
到目前为止,我有这样的:
<script>
$('div.ordergrid table.orders tbody tr.row td').click(
function() {
$(this).parent().parent().children('tr.rowdetail').toggle();
}
)
</script>
<div id="ordergrid">
<table id="orders">
<thead>
<th>Foo</th>
<th>Foo</th>
<th>Foo</th>
<th>Foo</th>
<th colspan="3">Foo</th>
<th>Foo</th>
</thead>
@foreach (var mo in Model)
{
<tbody>
<tr class="row">
<td>Bar</td>
<td>Bar</td>
<td>Bar</td>
<td>Bar</td>
<td colspan="3">Bar</td>
<td>Bar</td>
</tr>
<tr class="rowdetail">
<td colspan="8">
<table>
<thead>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
<th>Baz</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
}
</table>
</div>
(嵌套TBODY稍后将与其他的foreach代替。)
目前,什么都不会发生在点击。点击事件似乎没有“粘”到预期的控制。
哦,.parent().parent()
看起来有点傻,但我不确定如何正确选择.parents()
。
您是否看到错误?如果您只是在click事件上使用console.log,会发生什么情况?如果可行,请尝试记录'.parent()。parent()。children('tr.rowdetail')'返回的内容。它是否返回任何/正确的元素?另外,在将click元素绑定到DOM时,DOM中是否存在这些元素?如果没有,请查看https://api.jquery.com/on/。 –