简体   繁体   English

使用单元格值作为参数将javascript函数应用于表的每一行?

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

I have a table (generated from a php array) that shows a users current auctions. 我有一张表格(从php数组生成),显示用户当前的拍卖。 One of the columns of this table shows the time remaining in the auction. 该表的其中一列显示拍卖剩余时间。 I am using a jquery countdown plugin ( http://www.keith-wood.name/countdown.html ) that makes the remaining time countdown to zero. 我正在使用一个jquery倒数插件( http://www.keith-wood.name/countdown.html ),该插件可将剩余时间倒数为零。

How can I apply the countdown function to every row using the different remaining times? 如何使用剩余时间将倒数功能应用于每一行?

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

what I am trying, but doesn't seem to be working: 我在尝试什么,但似乎没有用:

 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>';
     }
}

Use each to loop through all the table row. 使用每个循环遍历所有表行。 Do not write JS script with PHP. 不要用PHP编写JS脚本。

Create table structure like this defining the id with your incremental i variable: 像这样创建表结构,用增量i变量定义id:

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

And JS code like that : 像这样的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