为什么我会在这上面得出错误的结果。计算错误,无法创建空输入文本警报

问题描述 投票:2回答:1

我是HTML与JavaScript结合的新手。这是我的功课和我的代码有一些问题需要专家来看看。这是个问题。

如果工作时间大于40小时,则使用1.5倍的常规费率计算额外时间。例如,如果一个工作50小时并且支付的费率是10.00美元/小时,则总工资应为550.00美元。

我的代码:

<!DOCTYPE html>
<html>

<head>
  <script type="text/javascript">
    function calc() {
      var employ_name = document.getElementById('employ_name').value;
      var rate_pay = parseInt(document.getElementById('rate_pay').value);
      var hour_work = parseInt(document.getElementById('hour_work').value);
      if (hour_work <= 40) {
        alert("Paid Amount For " + employ_name + " is RM " + rate_pay * hour_work);
      } else {
        alert((40 * 1.5) + (c - 40) * c * 1.5);
      }
    }

    function calc() {
      if (document.getElementById("employ_name").value == "") {
        alert("PLEASE ENTER THE VALUE!");
        return false;
      } else if (document.getElementById("employ_no").value == "") {
        alert("PLEASE ENTER THE VALUE!");
        return false;
      } else if (document.getElementById("rate_pay").value == "" {
          alert("PLEASE ENTER THE VALUE!");
          return false;
        } else {
          alert("PLEASE ENTER THE VALUE!");
          return false;
        }
      }
  </script>
  <title></title>
</head>

<body>

  <form action="url" METHOD="POST">
    <table>
      <tr>
        <td>Employee Name :</td>
        <td><input type="text" id="employ_name"></td>
      </tr>
      <tr>
        <td>Employee No :</td>
        <td><input type="text" id="employ_no"></td>
      </tr>
      <tr>
        <td>Rate of Pay :</td>
        <td><input type="text" id="rate_pay"></td>
      </tr>
      <tr>
        <td>Hours Work :</td>
        <td><input type="text" id="hour_work"></td>
      </tr>
      <tr>
        <td><input type="button" id="Button" value="Submit" onclick="calc()"></td>
      </tr>

    </table>

  </form>

</body>

</html>
javascript html
1个回答
1
投票

您的代码存在一些问题:

  1. 您的语法目前不正确,您需要通过在if语句中添加结束括号来解决此问题: (document.getElementById("rate_pay").value == "") {...}
  2. 您有两个具有相同名称的函数。相反,你需要给你的第二个函数一个不同的名字,比如checkInputs,这样当你调用calc函数时,它会调用预期的函数。
  3. 如果所有calc失败,你的第二个checkInputs函数(false)不应该返回if-statments。相反,你应该返回true,如果他们都fail,因为这意味着他们都不是空的。
  4. 要使你的calc函数检查你的所有输入都有一个值,你可以在你的第一个calc函数中调用你的第二个checkInputs函数(现在calc)。这样你就可以使用if语句检查checkInputs是否返回,从而采取相应的行动。

请参阅以下修订代码:

<!DOCTYPE html>
<html>

<head>
  <script type="text/javascript">
    function calc() {
      if (checkInputs()) { // check if `checkInputs` returned true or false
        var employ_name = document.getElementById('employ_name').value;
        var rate_pay = parseInt(document.getElementById('rate_pay').value);
        var hour_work = parseInt(document.getElementById('hour_work').value);
        if (hour_work <= 40) {
          alert("Paid Amount For " + employ_name + " is RM " + rate_pay * hour_work);
        } else {
          // commented out as you have not show us what "c" is:
          // alert((40 * 1.5) + (c - 40) * c * 1.5);
        }
      } else { // if check inputs returned false then you can output error message
        alert("PLEASE ENTER THE VALUE!");
      }
    }

    function checkInputs() {
      if (document.getElementById("employ_name").value.trim() == "") {
        return false;
      } else if (document.getElementById("employ_no").value.trim() == "") {
        return false;
      } else if (document.getElementById("rate_pay").value.trim() == "") {
        return false;
      }
      return true;
    }
  </script>
  <title>Calculator</title>
</head>

<body>

  <form action="url" method="POST">
    <table>
      <tr>
        <td>Employee Name :</td>
        <td><input type="text" id="employ_name"></td>
      </tr>
      <tr>
        <td>Employee No :</td>
        <td><input type="text" id="employ_no"></td>
      </tr>
      <tr>
        <td>Rate of Pay :</td>
        <td><input type="text" id="rate_pay"></td>
      </tr>
      <tr>
        <td>Hours Work :</td>
        <td><input type="text" id="hour_work"></td>
      </tr>
      <tr>
        <td><input type="button" id="Button" value="Submit" onclick="calc()"></td>
      </tr>

    </table>

  </form>

</body>

</html>

注意:在你的checkInputs()方法中,我也使用过.trim()。这将删除输入上的前导/尾随空格。这意味着如果有人要进入空间,那么checkInputts将返回false而不是true

© www.soinside.com 2019 - 2024. All rights reserved.