我有一个在循环中生成的表,如下所示。在我的代码中,内部循环完成执行(即完成显示一组行)后会向 赋予新的 id,因此 id 是以集合的形式给出的,而不是单独的行。我的问题是如何计算具有唯一 id 的总行数或行数/行集。我知道我可以通过以下方式获取行总数:
var rowCount = $('#riskForm tr').length;
这是我的代码:
<table width="850" bgcolor="white" cellpadding="3" cellspacing="0" align="center" border="0" class="riskForm" id="riskForm">
<thead>
<th> i </th>
<th style="text-align:center;" colspan="2"> Importance?</th>
<th> Equal </th>
<th> How Much More </th>
</thead>
for ($i=0; $i < (sizeOf ($myArray)); $i++){ //loop through the whole table body
$currentObs = $myArray[$i]['ObstacleDescription']; //set pointer to my current obstacle value
$x = 0;
for ($j=1; $j <= (sizeOf($myArray)-1); $j++){ //loop through the inner table
$next = $myArray[$j]['ObstacleDescription'];
$or = " or ";
if (!($currentObs == $next)){
?>
<tr id="<?php echo $myArray[$i]['ComplianceID']; ?>">
<td valign='center'> <?php echo $x+1; ?> </td>
<?php $x++; ?>
<td>
<?php echo
"<input type='radio' name='op[$j]' value='0' class='myradio'>"." ".$currentObs
?>
</td>
<td><?php echo
$or." "."<input type='radio' name='op[$j]' value='1' class='myradio'>"." ".$next;
?>
</td>
<td><small>
<?php echo " 1 "."<input type='radio' name='Intense[$j]' value='1' class='myradio'>"; ?>
</small></td>
<td><small>
<?php echo
" 2 "."<input type='radio' name='Intense[$j]' value='2' class='myradio'>".
" 3 "."<input type='radio' name='Intense[$j]' value='3' class='myradio'>".
" 4 "."<input type='radio' name='Intense[$j]' value='4' class='myradio'>".
" 5 "."<input type='radio' name='Intense[$j]' value='5' class='myradio'>".
" 6 "."<input type='radio' name='Intense[$j]' value='6' class='myradio'>".
" 7 "."<input type='radio' name='Intense[$j]' value='7' class='myradio'>".
" 8 "."<input type='radio' name='Intense[$j]' value='8' class='myradio'>".
" 9 "."<input type='radio' name='Intense[$j]' value='9' class='myradio'>";
?>
</small></td>
<?php }
}
echo "<tr><td colspan='5'><hr style='border:0; height:0px' /></td></tr>";
echo "<tr><td colspan='5'><hr style='border:0; height:0px' /></td></tr>";
}
echo "</tr>";
?>
?>
</tbody>
</table>
我还想使用 jquery 获取每个选定行的单选选项。
最佳答案
由于所有 ID 都必须是唯一的 data-row_id
属性 <tr>
你可以这样做:
JS
var rows=[];
$('#riskForm tr').each(function(){
var id=$(this).data('row_id');
if( rows.indexOf( id) ==-1){ /* push to array if it doesn't exist */
rows.push( id)
}
});
var totalUnique= rows.length
HTML
<tr data-row_id="654">
关于 radio 选项的问题不是很清楚
关于javascript - 如何获取表 <tr> 的唯一行并使用 jquery 对它们进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461172/