未捕获的引用错误javascript

时间:2016-11-15 15:45:37

标签: javascript html

我正在处理一个表单,该表单从用户和下拉列表中获取值以计算值并将其显示在空表单字段中。这还需要货币格式功能来显示美元的价值。除了货币格式,我可以让一切工作得很好。

代码现在的显示方式为uncaught reference error。如果我从inNum事件中删除onClick函数名称,则if语句的第一部分有效,显示“Invalid Amount”。

非常感谢任何有关这个难题的帮助!谢谢!

function getShipping(inNum) {
  var product = document.getElementById("selected").value;
  var quantity = document.getElementById("textfield2").value;

  var salesT = product * quantity;

  var taxRate = document.getElementById("state").value;

  var taxTotal = taxRate * salesT;

  var shipping = document.getElementById("shipping").value;

  var totalBill = parseFloat(taxTotal) + parseFloat(salesT) + parseFloat(shipping);

  if (isNaN(inNum)) {
    //alert("result of isNaN" );   //The input amount is a non numeric string. It is or contains letters and/or spaces
    document.getElementById("total").value = "Invalid amount"
  } else {
    inNum = parseFloat(totalBill.inNum); //Convert input value into a floating point number.  toFixed() requires a number value to work with
    document.getElementById("total").value = parseFloat(totalBill) + inNum.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
  }

  //document.getElementById("total").value = "$" + totalBill;
}
<h1 align=center>Calculate Your Order</h1>
<form name="frmProd" method="post" action="">
  <table border="1">
    <tr>
      <td width="53%">Select Product:</td>
      <td width="47%">
        <select size="1" name="product" id="selected">
          <option value="0" selected>Please Select a Product</option>
          <option value="1.99">Book</option>
          <option value=".99">Pen</option>
          <option value=".25">Pencil</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Quantity:</td>
      <td>
        <input name="textfield2" type="text" id="textfield2" size="5">
      </td>
    </tr>
    <tr>
      <td>Shipping State (Tax Purposes):</td>
      <td>
        <select size="1" name=state id="state">
          <option selected value="0.06">Iowa</option>
          <option value="0.085">Illinois</option>
          <option value="0.7">Indiana</option>
          <option value="0.0">Other</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Delivery Method:</td>
      <td>
        <select size="1" name="shipping" id="shipping">
          <option value="9.50" selected>Standard UPS - 3 Days</option>
          <option value="6.50">US Mail - 5 Days</option>
          <option value="19.95">Overnight - 1 Day</option>
          <option value="0.0">Pickup</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Your Total is:</td>
      <td>
        <input name="total" value=$ 0.00 id="total">
      </td>
    </tr>
  </table>
  <p>
    <input type="button" value="Calculate My Order Now!" name="submit" onClick="getShipping(inNum)">
    <input type="reset" name="Reset" id="button" value="Reset Form">
  </p>
</form>

1 个答案:

答案 0 :(得分:1)

<a href='#/' id='trig1'>HOVER</a>
<iframe id='ifrm1' name='ifrm1' src='https://days.to/until/christmas'></iframe>
此时

inNum = parseFloat(totalBill.inNum); 是一个数字,因此它没有对totalBill的引用。

我不确定你要对inNum做什么,但完全摆脱它可能是你正在寻找的解决方案:

inNum
function getShipping() {
  var product = document.getElementById("selected").value;
  var quantity = document.getElementById("textfield2").value;

  var salesT = product * quantity;

  var taxRate = document.getElementById("state").value;

  var taxTotal = taxRate * salesT;

  var shipping = document.getElementById("shipping").value;

  var totalBill = parseFloat(taxTotal) + parseFloat(salesT) + parseFloat(shipping);

  if (isNaN(totalBill)) {
    //alert("result of isNaN" );   //The input amount is a non numeric string. It is or contains letters and/or spaces
    document.getElementById("total").value = "Invalid amount"
  } else {
    document.getElementById("total").value = parseFloat(totalBill).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
  }

  //document.getElementById("total").value = "$" + totalBill;
}