用JavaScript计算百分比。

时间:2018-10-30 11:06:32

标签: javascript math percentage

以下代码运行正常。 SecondNumVal是25,但在percentVal中,它在20分上给出100%,在25分上给出125%。我找不到错误,为什么它没有在25分上给出100%,在20分上给出80%。

$('#marks').on('change', '.obtmark', function() {
  var SecondNumVal = "25";
  $mark = $(this)
  var firstNumVal = $mark.val();
  // Find the tr this control is in and the corresponding percentage field
  $row = $mark.closest('tr')
  $pct = $row.find('.percentage')
  percentVal = (firstNumVal / SecondNumVal) * 100
  pct = parseInt(percentVal) + '%';
  $pct.val(pct).attr('readonly', true);

  $studentgrade = $row.find('.grades')
  $remarks = $row.find('.remark')
  if (percentVal >= 90 && percentVal <= 100) {
    calculatedgrade = "A+";
    remark = "Excellent";
  } else if (percentVal >= 80 && percentVal < 90) {
    calculatedgrade = "A";
    remark = "Very Good";
  } else if (percentVal >= 70 && percentVal < 80) {
    calculatedgrade = "B";
    remark = "Good";
  } else if (percentVal >= 60 && percentVal < 70) {
    calculatedgrade = "C";
    remark = "Good";
  } else if (percentVal >= 50 && percentVal < 60) {
    calculatedgrade = "D";
    remark = "STY";
  } else if (percentVal >= 40 && percentVal < 50) {
    calculatedgrade = "E";
    remark = "USTY";
  } else if (percentVal < 40) {
    calculatedgrade = "F";
    remark = "Fail";
  }
  $studentgrade.val(calculatedgrade).attr('readonly', true);
  $remarks.val(remark).attr('readonly', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input name='obtmarks[]' placeholder='' class='form-control obtmark' type='number' required='required' style='width: 120px;'></td>
    <td><input name='percentage[]' placeholder='' class='form-control percentage' type='text' required='required' style='width: 120px;'></td>
    <td><input name='grade[]' placeholder='' class='form-control grades' type='text' required='required' style='width: 120px;'></td>
    <td><input name='remarks[]' placeholder='' class='form-control remark' type='text' required='required' style='width: 120px;'></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

I am getting exact output if i enter 25 as input it is giving correct output as 100% pls go through attached picture.
    此代码有效。更改您的选择

$('.obtmark').change( function() {
      var SecondNumVal = "25";
      $mark = $(this)
      var firstNumVal = $mark.val();
      // Find the tr this control is in and the corresponding percentage field
      $row = $mark.closest('tr')
      $pct = $row.find('.percentage')
      percentVal = (firstNumVal / SecondNumVal) * 100
      pct = parseInt(percentVal) + '%';
      $pct.val(pct).attr('readonly', true);

      $studentgrade = $row.find('.grades')
      $remarks = $row.find('.remark')
      // calculatedgrade="";
      // remark="";
      if (percentVal >= 90 && percentVal <= 100) {
        calculatedgrade = "A+";
        remark = "Excellent";
      } else if (percentVal >= 80 && percentVal < 90) {
        calculatedgrade = "A";
        remark = "Very Good";
      } else if (percentVal >= 70 && percentVal < 80) {
        calculatedgrade = "B";
        remark = "Good";
      } else if (percentVal >= 60 && percentVal < 70) {
        calculatedgrade = "C";
        remark = "Good";
      } else if (percentVal >= 50 && percentVal < 60) {
        calculatedgrade = "D";
        remark = "STY";
      } else if (percentVal >= 40 && percentVal < 50) {
        calculatedgrade = "E";
        remark = "USTY";
      } else if (percentVal < 40) {
        calculatedgrade = "F";
        remark = "Fail";
      }
      $studentgrade.val(calculatedgrade).attr('readonly', true);
      $remarks.val(remark).attr('readonly', true);
    });