JavaScript函数&方程

时间:2014-11-18 18:36:35

标签: javascript forms math

我试图使用JavaScript和Compute函数完成输入和数字的简单乘法问题(5.49)。我把它放在一个表中,并使用内部css作为页面的结构。 我的问题是,当我按下页面上的计算按钮时,等式(输入)* 5.49的答案没有出现,但没有任何结果。我在努力寻找错误。 附加说明:我已经检查过计算按钮是否正常工作,方法是用弹出框替换公式并且它正常工作。

<!DOCTYPE html>
<html>

<head>
  <title>Project</title>
  <style type="text" /css>
    .inbox { width=30px; text-align: right; border: 2px solid black; } .align { text-align: right }
  </style>
  <script type="text/javascript">
    function compute() {
      var a = form1.inputA.value;
      a = parseFloat(a);
      var b = form1.inputB.value;
      b = parseFloat(b);
      var c = form1.inputC.value;
      c = parseFloat(c);
      var e = a * 5.49;
      form1.sumA.value = e.toFixed(2);
    }

    function pageInit() {
      form1.inputA.focus();
    }
  </script>
</head>

<body onload="pageInit();">
  <form id="form1">
    <table border="2">
      <tr>
        <th colspan="4">Sample Order Form</th>
      </tr>
      <tr>
        <th>Quantity</th>
        <th>item</th>
        <th>Unit Price</th>
        <th>Totals</th>
      </tr>
      <tr>
        <th>
          <input tabindex="1" class="inbox" type="text" id="inputA" />
        </th>
        <th>Apples</th>
        <td>$5.49</td>
        <th>
          <input class="inbox" type="text" id="sumA" readonly="readonly" />
        </th>
      </tr>
      <tr>
        <th>
          <input tabindex="4" type="button" value="Compute" onclick="compute();" />
        </th>
      </tr>
    </table>
  </form>
</body>

</html>

1 个答案:

答案 0 :(得分:3)

字段inputBinputC不存在。然后它会产生错误。

function compute() {
   var a = form1.inputA.value;
   a = parseFloat(a);
   var e = a * 5.49;
   form1.sumA.value = e.toFixed(2);
}

您可以使用Web开发人员工具分析代码。 现代网络浏览器有这样的工具。 看一下谷歌浏览器开发人员工具:https://developer.chrome.com/devtools。您可以找到firefox和Internet Explorer的等效工具。