簡體   English   中英

使用單元格值作為參數將javascript函數應用於表的每一行?

[英]Apply javascript function to every row of a table using the cell value as a parameter?

我有一張表格(從php數組生成),顯示用戶當前的拍賣。 該表的其中一列顯示拍賣剩余時間。 我正在使用一個jquery倒數插件( http://www.keith-wood.name/countdown.html ),該插件可將剩余時間倒數為零。

如何使用剩余時間將倒數功能應用於每一行?

$(function () {
    $('#countdown').countdown({until: +remainingTime, format: 'HMS', compact: true});
});

我在嘗試什么,但似乎沒有用:

 foreach($tradePile['auctionInfo'] as $auction)
 {                      
     if ($auction['tradeState'] == "active")
     {
        $i++;
        echo '<tr>';
        echo '<td><a href="player.php?id='.$auction['itemData']['resourceId']. '">'.$stats['first_name'].' '.$stats['last_name'].'</a></td>';
        echo'<td>'.$auction['itemData']['rating'].'</td>';
        echo'<td>'.$buyNowPrice.'</td>';
        echo'<td>'.$auction['startingBid'].'</td>'; 
        //remaining time for each auction:
        //$auction['expires']
        ?>
        <td>
           <script>$(function () {$('#countdown<?php echo $i; ?>').countdown({until: +<?php echo $auction['expires'] ?>, format: 'HMS', compact: true});});</script>
           <div id="countdown<?php echo $i; ?>"></div>
        </td>
        <?php
        echo'</tr>';
     }
}

使用每個循環遍歷所有表行。 不要用PHP編寫JS腳本。

像這樣創建表結構,用增量i變量定義id:

<table id="myTable">
    <tr id="1" expire="55">
    <td id="countdown-1">....</td>
        ...
    </tr>
</table>

像這樣的JS代碼:

$(function () {
    $('#myTable tr').each(function(){
        var remainingTime = $(this).attr('expire');
        var id = $(this).attr('id');
        $('#countdown-'+id).countdown({until: +remainingTime, format: 'HMS', compact: true});
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM