我想使用jQuery在每行中将分数分配给不同的分数

时间:2018-11-15 22:28:32

标签: javascript php mysql

下面是我的代码

<tbody>
<?php
if($query->rowCount() > 0)
{
  while($row = $query->fetch(PDO::FETCH_ASSOC))
    {
      ?>
<tr class="rows">
    <td class='f_name'><?php echo $row['fullnames']; ?></td>
    <td>
      <input type="text" class='avg' value="<?php echo $row['average']; ?>">
    </td>
    <td><input type='text' class='grade' value=""></td>
</tr>
<?php
     }
 }else {echo "No Records Found";}
?>
</tbody>
我希望每个平均值都有其相应的等级。下面是我的JavaScript代码

$(function(){
    var rw = $('.avg').val();
    var st;
    if(rw >= 80.00 && rw <= 100){st = 'Excellent';}
    else if(rw >= 70.00 && rw <= 79.99){st = 'Very Good';}
    else if(rw >= 60.00 && rw <= 69.99){st = 'Good';}
    else if(rw >= 50.00 && rw <= 59.99){st = 'Credit';}
    else if(rw >= 40.00 && rw <= 49.99){st = 'Passed';}
    else if(rw >= 0.00 && rw <= 39.99){st = 'Failed';}
    else {st = 'No Grade';}
    $('.cposts').val(st);
});

请问我在做什么错

1 个答案:

答案 0 :(得分:1)

它针对该类的所有元素。但是,您只需要该行以及同一行中的相应元素。对于每个<tr>,我都会按行进行:

$(function() {
  $(".rows").each(function() {
    var rw = $(this).find('.avg').val();
    var st;
    if (rw >= 80.00 && rw <= 100) {
      st = 'Excellent';
    } else if (rw >= 70.00 && rw <= 79.99) {
      st = 'Very Good';
    } else if (rw >= 60.00 && rw <= 69.99) {
      st = 'Good';
    } else if (rw >= 50.00 && rw <= 59.99) {
      st = 'Credit';
    } else if (rw >= 40.00 && rw <= 49.99) {
      st = 'Passed';
    } else if (rw >= 0.00 && rw <= 39.99) {
      st = 'Failed';
    } else {
      st = 'No Grade';
    }
    $(this).find('.cposts').val(st);
  });
});