2013-12-10 184 views
1

我想遍歷我的錶行,但我總是有一個錯誤(無法識別的表達式)。 每個函數被調用時都會出現錯誤。循環遍歷錶行

我該如何解決這個問題?

在此先感謝

這裏是我的代碼:

jQuery的

$("#searchValue").keyup(function() { 

      var table = $(this).siblings('table').not(':hidden'); 
      $(table +" tr").each(function() { 

      }); 
}); 

HTML

<table id='tablePlanning' class='tablesorter'> 
    <thead> 
     <tr> 
      <th>PR Code</th> 
      <th>Klant</th> 
      <th>Description</th> 
      <th>Project status</th> 
      <th>Project Leader</th> 
      <th>Coordinator</th> 
      <th>Account manager</th> 
      <th>Billing</th> 
      <th>Start Datum</th> 
      <th>Hardware</th> 
     </tr> 
    </thead> 
    <tbody> 

    </tbody> 
</table> 

<table id='tableProject' class='tablesorter'> 
    <thead> 
     <tr> 
      <th>Project ID</th> 
      <th>Description</th> 
      <th>Customer</th> 
      <th>Status</th> 
      <th>Max Hours</th> 
      <th>Achieved</th> 
      <th>Difference</th> 
     </tr> 
    </thead> 
    <tbody> 

    </tbody> 
</table> 

回答

4

$(table +" tr")將採取通過table引用的jQuery對象,將其轉換爲字符串("[object Object]"),將" tr"附加到它,然後嘗試使用結果字符串"[object Object] tr"作爲選擇器。

你可能想find,這是jQuery的對象能夠使用的功能,搜索在jQuery對象中的元素中的子元素:

table.find('tr').each(/*...*/); 
0

沒有通過可見表循環。

循環遍歷可見表格並在每個表格內循環遍歷行。

嘗試:

$("#searchValue").keyup(function() { 
    $("table:visible").each(function() { 
     $(this).find("tr").each(function(){ 
      //do something 
     }); 
    }); 
}); 
+1

僅有代碼的答案很少有很好的答案。解釋發生了什麼,用戶做錯了什麼,如何改變它等等。 –

0

$( 「#searchValue」)KEYUP(函數(){

$('tr:visible','table.tablesorter:visible').each(function(){ 
    var trObj=$(this); 
    //-- your code goes here 
    }); 

});

使用選擇器的說明: 選擇所有可見表格中的所有可見表格,其類別爲「tablesorter」。 & $(this)將引用每個tr對象。