如何获取表的唯一行并使用jquery统计它们

时间:2013-12-09 00:35:02

标签: javascript jquery html mysql sql

我有一个循环生成的表,如下所示。使用我的代码,在内循环完成执行之后给出一个新的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">&nbsp;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获取每个选定行的无线电选项。

1 个答案:

答案 0 :(得分:1)

使用您data-row_id上的<tr>属性,所有ID必须是唯一的:

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">

关于无线电选项的问题不是很清楚