简体   繁体   English

展开/折叠表格行Javascript无法正常工作

[英]Expand/Collapse table row Javascript works wrongly

I'm using this code to expand database table row to reveal more information about the item, but it does exactly the opposite. 我正在使用此代码来扩展数据库表行以显示有关该项目的更多信息,但这恰恰相反。

    $("td span.expand").click(function() {
    $(this).parents("tr.main").nextUntil("tr.main"). toggle();
});

When I open the table all rows that must be hidden are being shown and if I click on any main row it will hide that rows. 当我打开表格时,所有必须隐藏的行都将显示,如果单击任何主行,它将隐藏该行。 How to invert this? 如何反转呢? Here's part of code I use for table (I know, it's messy): 这是我用于表格的一部分代码(我知道,这很混乱):

    echo '<table border="1" class="tabula">
<tr class="main">
<th width="30px"><a href="?page='.$page.'&sort=nr&sort_by='.$sort_order.'">Nr.</a></th>
<th width="75px"><a href="?page='.$page.'&sort=numurs&sort_by='.$sort_order.'">Mašīnas numurs</a></th>
<th width="75px"><a href="?page='.$page.'&sort=regnr&sort_by='.$sort_order.'">Reģistrācijas numurs</a></th>
<th width="75px"><a href="?page='.$page.'&sort=modelis&sort_by='.$sort_order.'">Marka / Modelis</a></th>
<th width="75px"><a href="?page='.$page.'&sort=apzim&sort_by='.$sort_order.'">ID</a></th> 
</tr>';

    //Ieraksta datus tabulā
while($row = mysql_fetch_array($query)) 
  {
  echo "<tr class='main'>";
    echo "<td width='30px'>" . $row['nr'] . "</td>";
    echo "<td width='75px'>" . $row['numurs'] . "</td>";
    echo "<td width='75px'><span class='expand'>" . $row['regnr'] . "</span></td>";
    echo "<td>" . $row['modelis'] . "</td>";
    echo "<td>" . $row['apzim'] . "</td>";
  echo "</tr>";
  echo "<tr class='hidden'>";
    echo "<td>text</td>";
    echo "<td>text</td>";
    echo "<td>text</td>";
  echo "</tr>";
  }
echo "</table>";
echo "<br>";

Try using CSS in your file and include the following: 尝试在文件中使用CSS并包括以下内容:

<style>
.expand
{
display:none;
}
</style> 

I use a jQuery-based toggle script, and that's what I used when I had a similar problem such as this. 我使用基于jQuery的切换脚本,这是我遇到类似问题时使用的方法。

You may need to use .hidden { display:none; } 您可能需要使用.hidden { display:none; } .hidden { display:none; } instead. .hidden { display:none; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM