为什么我不能使用JQuery隐藏和显示元素?

时间:2013-03-14 06:16:54

标签: php jquery

这是我正在渲染表格行的Php代码块: -

实际上我想要实现的是,在单击第一行中的按钮时,我应该能够显示下一行,这是先前在文档就绪脚本中使用.hide()隐藏的。

     <?php
         echo "<tr>";
            echo "<td>"."<button id= \"btnnumber_". $i ." \"  class=\"btn info toggler\" data-value=\" $val\">Show Info  <i class=\"icon-arrow-down\"></i></button>"."</td>"; // On click of this button I am taking its id in Jquery getting the number at end creating the id of the next row in the Jquery script.
         echo "</tr>";

         echo "</tr>";
            echo "<tr id=\"row_$i \" class=\"info_row\">";// This row is dynamically generated one each row has its unique id like 0 row is having id row_0,1 row is having id row_1 etc.
            echo "<td id=\"student_count\">"."students count:"."</td>";
            echo "<td id=\"start_date\">"."start date: "."</td>";
            echo "<td id =\"end_date\">"."end date: "."</td>";
            echo "<td></td>";
        echo "</tr>";

 ?>

此行最初设置为使用以下JQuery隐藏在文档中: -

          $(function(){

              $('.info_row').hide();// On document load I am hiding all the element with class info_row.


              $('.toggler').toggle(function(){// Then I am toggling between hide and show.

               var currentId = $(this).attr('id');
               var number = currentId.substr(currentId.length - (currentId.length - currentId.indexOf("_") - 1));
               var rowId = 'row_' + number;
               $("#" + rowId).show();
             }, function(){

              var currentId = $(this).attr('id');
              var lastChar = currentId.substr(currentId.length - (currentId.length - currentId.indexOf("_") - 1));
              var rowId = 'row_' + lastChar; 
              $("#" + rowId).hide();

      });  
    });

我无法实现切换,即行没有隐藏和显示,正如我试图实现的那样。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

此行看起来像一个问题

 echo "<tr id=\"row_$i \" 

因为id中存在虚假空间。

echo "<tr id=\"row_$i\"