【问题标题】:Why I cant hide and show an element using JQuery?为什么我不能使用 JQuery 隐藏和显示元素?
【发布时间】:2013-03-14 06:16:53
【问题描述】:

这是我在其中渲染表格行的 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();

      });  
    });

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

我们将不胜感激。

【问题讨论】:

  • .toggler 类应用在哪里?
  • 该类是 php 生成的另一个表行的一部分,我也将添加该部分。对不起。
  • html在页面上后jquery要初始化
  • 我添加了包含切换器类的行。 @Hello_world 我将我的 jQuery 放在文档就绪块中。这还不够吗。
  • jQuery 是一个客户端框架。不要强迫我们解析所有的 php.ini 文件。发布重现问题的相关 HTML 的(简化)片段。如果可能,在jsfiddle.net 上创建一个演示。帮助我们帮助您。

标签: php jquery


【解决方案1】:

这条线看起来有问题

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

id 中有一个虚假的空格。

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

【讨论】:

  • 你修好了,先生。非常感谢,关注细节是真正黑客的标志。再次感谢先生
  • 所以@SteveP 我们在编写脚本时应该非常小心,任何虚假的空间都可能会引起不必要的头痛。我的结论正确吗?
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多