javascript - 如何获取表 <tr> 的唯一行并使用 jquery 对它们进行计数

标签 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 获取每个选定行的单选选项。

最佳答案

由于所有 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/

相关文章:

javascript - 捕获输入内的值变化

html - 文本区域无法解释的间距

javascript - __flash__removeCallback is undefined errors in IE9 while removed the dom element

javascript - 如何在 Node 中解析 HTTP 状态码?

javascript - 如何在 javascript 中自定义 <span>?

html - 悬停 DIV 时 CSS 更改图像不起作用

javascript - 元素的外观仅在第二次单击时发生变化

javascript - 将元素 append 到特定 ID、for 循环和匿名函数?

javascript - 如何使用 php 和 jquery 更改带有选择选项的 sql 顺序值

jquery - 具有 [属性 ="value"] 规则的 CSS 不适用于 jquery-svg 图像