在.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/。 –