【问题标题】:calculating percentage in Javascript.在 Javascript 中计算百分比。
【发布时间】:2019-04-03 09:58:19
【问题描述】:

以下代码运行良好。 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>

【问题讨论】:

  • 从结果看来,分子和分母有些混淆。您能否为firstNumVal 添加日志记录?
  • #marks是哪个元素?
  • 附带说明:jQuery 中没有计算百分比这样的东西。它只是一个使用Javascript 的库。您应该相应地标记您的问题。
  • 您还可以轻松地将数学部分提取到一个不关心 DOM 的纯函数中,并在 DOM 更新回调中使用它。
  • 我没有发现您发布的代码有任何问题,可能问题出在其他地方,请记住,您必须使用 parseInt 转换所有输入值,因为这些是字符串值具体来说,如果您这样做,那么它将自动适用于所有网络浏览器。但如果你不这样做,那么行为可能会有所不同。请在 chrome 中测试您的代码,如果在 chrome 中测试,则在 Firefox 中进行确认。

标签: javascript math percentage


【解决方案1】:

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);
    });

【讨论】:

  • 同样的结果。当用户在输入框中输入 25 个标记时,其计算为百分比输入框中的 125%。
  • 对我来说,当我在第一个输入框中输入 25 时,我得到 100%。请检查一下
  • 请找到js小提琴链接
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 2017-02-08
  • 1970-01-01
  • 2015-09-07
  • 2012-09-20
  • 2011-06-01
  • 2021-01-10
相关资源
最近更新 更多