未捕获的引用错误

Uncaught reference error javascript

本文关键字:引用 错误      更新时间:2023-09-26

我正在制作一个表单,该表单从用户和下拉数组中获取值,以计算值并将其显示在空表单字段中。这还需要一个货币格式函数来显示以美元表示的值。我可以让一切工作正常,除了货币格式。

代码现在显示的是uncaught reference error。如果我从onClick事件中取出inNum函数名,则If语句的第一部分工作,显示"无效金额"。

对于这个难题的任何帮助都是非常感激的!谢谢你!

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>

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;
}
<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()">
    <input type="reset" name="Reset" id="button" value="Reset Form">
  </p>
</form>