未捕获的引用错误
Uncaught reference error javascript
我正在制作一个表单,该表单从用户和下拉数组中获取值,以计算值并将其显示在空表单字段中。这还需要一个货币格式函数来显示以美元表示的值。我可以让一切工作正常,除了货币格式。
代码现在显示的是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>
相关文章:
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- Wordpress中的Javascript出现匿名函数/引用错误
- 使用dc.js、d3.js和crossfilter引用错误
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- $未定义-未捕获引用错误
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- 我该如何解决“;未捕获引用错误:谷歌没有定义"?(谷歌地图API)
- 调用setTimeout内部的函数时发生引用错误
- 未捕获的引用错误:未定义<映射键>
- Javascript错误:未捕获引用错误:未定义编辑
- Google可视化addListener调用函数引用错误
- Cordova:未捕获引用错误:chbrody没有使用SQLitePlugin.js定义PhoneGap
- 未捕获引用错误:未定义PrintThisPage
- 引用错误:在尝试使用ajax发送数据时未定义
- 包含的文件中引发引用错误..除非调用文件包含慢速代码
- 在表单提交按钮的点击事件中调用函数时发生引用错误
- 未捕获引用错误:幻灯片未定义