2013-03-14 81 views
1

這裏是我的PHP代碼塊,其中我渲染表格行: -爲什麼我不能使用JQuery隱藏和顯示元素?

其實我想要實現的是,在第一行中按鈕的點擊,我應該能夠顯示下一個行,這是以前在文檔就緒腳本中使用.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(); 

     }); 
    }); 

我不能夠實現切換,即該行不隱藏和顯示,因爲我是試圖實現。

任何幫助將不勝感激。

+0

'.toggler'類應用於哪裏? – 2013-03-14 06:19:27

+0

該類是由php生成的另一個錶行的一部分,我也將添加該部分。抱歉。 – 2013-03-14 06:21:14

+0

jquery必須在html頁面上進行初始化 – 2013-03-14 06:22:28

回答

3

這一行看起來像一個問題

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

在有在ID虛假的空間。

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

你修好了。非常感謝,注重細節是真正的黑客的標誌。再次感謝 – 2013-03-14 06:40:57

+0

因此,@SteveP我們應該非常小心地編寫腳本,任何虛假的空間都可能導致不必要的頭痛。我的結論是否正確。 – 2013-03-14 06:43:42

+0

歡迎您。 , – SteveP 2013-03-14 06:45:22