javascript-如何使用html表单进行区域计算

时间:2016-08-18 09:32:25

标签: javascript jquery html html-form-post

我需要计算一个房间的面积,我无法找到我的代码中的问题。更具体地说,我需要从表单中调用值,当我按下提交按钮时,我想根据我的计算打印房间区域。

getElementById似乎也不起作用,我也不知道为什么。



function area() {
  var perimeter = document.getElementById("PE").value;
  var height = document.getElementById("He").value;
  var doors = document.getElementById("Nd").value;
  var doors_w_par = doors * 5.0212;
  var windows = document.getElementById("Nw").value;
  var windows_w_par = windows * 2.488;
  var closets = document.getElementById("Nc").value;
  var closets_w_par = closets * 1.37;
  var areas = (perimeter * height) - doors_w_par - windows_w_par - closets_w_par;
  return areas;
}
console.log(area());

<form action="" method="POST" id="myForm">
  Perimeter:
  <input type="float" min="0" max="9999999999999" id="Pe" name="P" />
  <br/>height:
  <input type="float" min="0" max="9999999999999" id="He" name="H" />
  <br />doors:
  <input type="number" min="0" max="9999999999999" id="Nd" name="D" />
  <br />windows:
  <input type="number" min="0" max="9999999999999" id="Nw" name="W" />
  <br />closets:
  <input type="number" min="0" max="9999999999999" id="Nc" name="C" />
  <br />
  <input type="submit" value="calc" name="sub" id="subm" onClick="testResults(this.form)">
</form>
<br />
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

  1. 在表单存在之前,您无法调用区域
  2. 您不需要使用提交按钮,而是使用type =“button”
  3. PE是您的形式的Pe
  4. 我不相信表单元素可以有type =“float” - “数字”是您可能想要的或“文本”或将步骤属性添加到数字
  5. 您需要根据所做的计算来解析返回的值
  6. 使用toFixed(小数)来舍入结果
  7. function area() {
      var perimeter = document.getElementById("Pe").value;
      perimeter = isNaN(perimeter)||perimeter==""?0:parseFloat(perimeter);
      var height = document.getElementById("He").value;
      height = isNaN(height)||height==""?0:parseFloat(height)
      var doors = document.getElementById("Nd").value;
      doors=isNaN(doors)||doors==""?0:parseInt(doors,10);
      var doors_w_par = doors * 5.0212;
      var windows = document.getElementById("Nw").value;
      windows=isNaN(windows)||windows==""?0:parseInt(windows,10);
      var windows_w_par = windows * 2.488;
      var closets = document.getElementById("Nc").value;
      closets=isNaN(closets)||closets==""?0:parseInt(closets,10);
       var closets_w_par = closets * 1.37;
      var areas = (perimeter * height) - doors_w_par - windows_w_par - closets_w_par;
      return areas.toFixed(2);
    }
    window.onload=function() {
      document.getElementById("subm").onclick=function() {
        console.log(area());
      }
    }
    <form action="" method="POST" id="myForm">
      Perimeter:
      <input type="number" step="0.01" min="0" max="9999999999999" id="Pe" name="P" />
      <br/>height:
      <input type="number" step="0.01" min="0" max="9999999999999" id="He" name="H" />
      <br />doors:
      <input type="number" min="0" max="9999999999999" id="Nd" name="D" />
      <br />windows:
      <input type="number" min="0" max="9999999999999" id="Nw" name="W" />
      <br />closets:
      <input type="number" min="0" max="9999999999999" id="Nc" name="C" />
      <br />
      <input type="button" value="calc" name="sub" id="subm" />
    </form>
    <br />