2012-11-21 35 views
4

我想添加一個倒計時計時器(使用這一個http://keith-wood.name/countdown.html)基於另一個td的文本使用jQuery在同一個表內的文本。添加倒數計時器到​​根據​​在表中的文本

例如我的表如下所示,如果狀態td包含文本「Received」,我想將計時器添加到time_remaining td。

<table> 
    <tr> 
     <td class="status">Completed</td> 
     <td class="time_received"></td> 
     <td class="time_remaining"></td> 
    </tr> 
    <tr> 
     <td class="status">Received</td> 
     <td class="time_received"></td> 
     <td class="time_remaining"></td> 
    </tr>   
</table> 

而且我的jQuery目前看起來像下面這樣,但我使用$(this).next()這隻會計時器添加到的time_received TD,但我需要將它添加到TIME_REMAINING TD,我可以使用,而不是.next()?任何幫助,將不勝感激。

var newCount = new Date(); 
newCount.setMinutes(newCount.getMinutes() + 30); 
$('td.status:contains(Received)').each(function() { 
    $(this).next().countdown({ 
     until: newCount, 
     format: 'M', 
     layout: "{mn} min" 
     }); 
​});​ 

回答

2

可以傳遞到下一個選擇和使用索引來指定哪些 - 在你的情況下,time_remaining類只有一個兄弟姐妹,所以它是一樣的

.next(".time_remaining")[0] 

或者,如果你總是希望第二個兄弟姐妹,你可以做

.nextAll()[1] 
+0

使用.nextAll('。time_remaining')工作正常。 – user1840724

2

.next()具有過載,需要一個選擇,所以你可以使用:

var newCount = new Date(); 
newCount.setMinutes(newCount.getMinutes() + 30); 
$('td.status:contains(Received)').each(function() { 
    $(this).next(".time_remaining").countdown({ 
     until: newCount, 
     format: 'M', 
     layout: "{mn} min" 
    }); 
}); 

在這裏看到:JQuery .next()

+0

乾杯的回覆,我已經嘗試過,在我的應用程序,並在jfiddle和定時器不會出現在 TD? 。 – user1840724

+0

如果我使用它,它工作$(this).nextAll(「。time_remaining」),但只使用.next(「。time_remaining」)不。 – user1840724

0

,您仍然可以使用。接下來(),只是通過在選擇你想要的TD。

$(this).next('.time_remaining').countdown({}); 

退房獲取更多信息的文檔: http://api.jquery.com/next/

+0

謝謝,但這似乎並沒有將計時器追加到time_remaining td? 。 – user1840724