Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在while循环中使用jquery显示/隐藏表行_Jquery_Loops_While Loop_Html Table_Tablerow - Fatal编程技术网

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

如何在while循环中使用jquery显示/隐藏表行,jquery,loops,while-loop,html-table,tablerow,Jquery,Loops,While Loop,Html Table,Tablerow,所以我有一个从数据库生成的带有while循环的表。数据按行显示。关键是存在“隐藏”行,其中的数据仅在单击时可用(如“显示详细信息”)。基本上我想要这样的东西:-如果单击表格行,下面会显示更多数据(在新行中) 我的代码如下所示: while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $display1=$display1+1; echo ' <tr class="tablerow"> <td&g

所以我有一个从数据库生成的带有while循环的表。数据按行显示。关键是存在“隐藏”行,其中的数据仅在单击时可用(如“显示详细信息”)。基本上我想要这样的东西:-如果单击表格行,下面会显示更多数据(在新行中)

我的代码如下所示:

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>';
/* Table row click */
$(".tablerow").click(function()
{
  $(".subtable").show();
}); 

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

$(".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() {
    // 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();
});​

我已成功地使用以下代码解决了此问题:

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>';
/* Table row click */
$(".tablerow").click(function()
{
  $(".subtable").show();
}); 

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

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

sushanth reddy帮了大忙,谢谢!我不太清楚为什么这个代码有效,而Sushanth却不行。某个地方可能有输入错误,但不管怎样,Susanth提供了正确的答案,因此我将用正确的代码编辑他的回复。

我已设法使用如下代码解决了此问题:

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>';
/* Table row click */
$(".tablerow").click(function()
{
  $(".subtable").show();
}); 

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

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

sushanth reddy帮了大忙,谢谢!我不太清楚为什么这个代码有效,而Sushanth却不行。可能在某个地方有输入错误,但无论如何,Susanth提供了正确的答案,因此我将用正确的代码编辑他的回复。

thx,但它不起作用。点击并没有像jquery代码是错误的,虽然看起来很好,我…thx,但它不工作。单击不会像jquery代码一样出错,尽管在我看来很正常。。。