在循环中切换表内的行

时间:2015-04-09 09:12:47

标签: php jquery html

我有一个带有“SHOW”文本的几个按钮的表格。每次我点击其中一个按钮时,我都会看到带有“.infotr”类的行,当我再次点击同一个按钮时,那些行就会消失。 (我的意思是点击按钮的同一个表的“.infotr”行。)

<?php 
$sql="SELECT * FROM mytable WHERE ID='$id'";
$res=mysqli_query($db,$sql);

while($row = mysqli_fetch_array($res))
{
    echo "<table id='tablemodificamobile'>";
    echo "<tr id='firsttr'>";
    echo "<td id='amid' class='modificatd'>".$myid."</td>";
    echo "<td id='amtitle' class='modificatd'>".$row['Title']."</td>";
    echo "<td id='amshow' class='modificatd'><input type='button' id='mostramodbtn' value='SHOW'></input></td>";
    echo "</tr>";
    echo "<tr class='infotr'><td class='addinfo'>New Price: ".$row['NewPrice']."</td></tr>";
    echo "<tr class='infotr'><td class='addinfo'>Old Price: ".$row['OldPrice']."</td></tr>";
    echo "<tr class='infotr'><td class='addinfo'><input type='button' value='SAVE' id='modit'></input></td></tr>";
    echo "</table>";
}
?>

这是我尝试的但它不起作用:

  $('#mostramodbtn').click(function() {
    $(this).parents("tr").nextAll().show();
  });

1 个答案:

答案 0 :(得分:0)

使用此:

$(document).on('click','#mostramodbtn',function() {
   $(this).parents("tr").nextAll().toggle("slow");
});

您正在动态创建元素,因此您需要使用.on函数绑定文档上的偶数。

详细了解.on() function in jQuery here

相关问题