如何在while循环中使用jquery显示/隐藏表行
所以我有一个使用while循环从数据库生成的表。 数据以行显示。 重点是有“隐藏”行,数据仅在点击时可用(如显示详细信息)。 基本上我想要这样的东西: http : //www.transfercar.co.nz/ – 如果你点击表格行,下面会显示更多数据(在新行中)。
我的代码如下所示:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $display1=$display1+1; echo ' ' . $row['id'] . ' ' . $row['name'] . '
' . $row['surname'] . '
' . $row['country'] . ' ' . $row['city'] . ' ' . $row['category'] . ' '; if($row['id']==$_GET['showrow']) { echo ' '; echo ' ';}} // End of WHILE loop. echo '';
我的jQuery看起来像这样:
/* Table row click */ $(".tablerow").click(function() { $(".subtable").show(); }); $(".subtable").click(function() { $(".subtable").hide(); });
点是一旦点击所有子表显示,自然我只想要点击表格行下面的一个..
试试这个
$(".tablerow").click(function() { // Hideing all the tr with class subtable rows initially $('.subtable').hide(); // Find the next tr which has class subclass in current context $(this).next('.subtable').show(); }); $(".subtable").click(function() { // Hide current row that has class subtable $(this).hide(); });
我设法使用这样的代码解决它:
$(".tablerow").click(function() { $(this).next(".subtable").slideDown("slow"); }); $(".subtable").click(function() { $(".subtable").hide(); });
sushanth reddy一直很有帮助,谢谢! 我不确定为什么这个代码有效,而Sushanth则不然。 在某处可能存在拼写错误,但无论如何,Susanth提供了正确答案,因此我将用正确的代码编辑他的回复。