如何在while循环中使用jquery显示/隐藏表行

问题描述:

所以我有一个使用while循环从数据库生成的表。数据以行显示。重点是有隐藏行,数据仅在点击时可用(如显示详细信息)。基本上我想要这样的东西: http://www.transfercar.co.nz/ - 如果点击表格行,下面会显示更多数据(新行)。

So I have a table generated from database with while loop. Data is displayed in rows. Point is that there are "hidden" rows with data available only on click (like show details). Basically I want something like this: http://www.transfercar.co.nz/ - if you click on the table row more data appears below (in new row).

我的代码如下所示:

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$display1=$display1+1;
    echo '
    <tr class="tablerow">
        <td>' . $row['id'] . '</td>
        <td><p class="state">' . $row['name'] . '</p></td>
        <td><p class="state">' . $row['surname'] . '</p></td>
        <td>' . $row['country'] . '</td>
        <td>' . $row['city'] . '</td>
        <td>' . $row['category'] . '</td>
    </tr>';
if($row['id']==$_GET['showrow'])
{
    echo '
    <tr class="subtable" style="display:none;">';
        echo '<td colspan="3" class="detalji"></td>
        <td align="left" colspan="3" class="detalji"></td>
        </tr>';}} // End of WHILE loop.
echo '</table>';

我的jQuery看起来像这样:

My jQuery looks like this:

/* Table row click */
$(".tablerow").click(function()
{
  $(".subtable").show();
}); 

$(".subtable").click(function()
{
  $(".subtable").hide();
}); 

点是一旦点击所有子表显示,我自然只想要点击表格行下方的那个。 。

Point is that once clicked all subtables show, naturally I only want the one below the clicked table row..

我设法用这样的代码来解决它:

I have managed to solve it using code like this:

$(".tablerow").click(function()
{
  $(this).next(".subtable").slideDown("slow");
}); 

$(".subtable").click(function()
{
  $(".subtable").hide();
});

sushanth reddy一直非常有帮助,谢谢!我不确定为什么这个代码有效,而Sushanth则不然。在某处可能有拼写错误,但无论如何,Susanth提供了正确的答案,因此我将使用正确的代码编辑他的回复。

sushanth reddy has been very helpful, thank you! I am not exactly sure why this code works and Sushanth is not. There is probably a typo somewhere, but anyway Susanth provided with correct answer and therefore i will edit his reply with correct code.