[英]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.