如果您查看我的最后一个javascript行| document.book.grandtotal.value = grandTotal。 toFixed(2); | 尝试运行我的代码后,它给我一个错误,指出“ grandTotal.toFixed不是函数” 如果我尝试删除toFixed,它将正常工作。我不知道这对所有行都起作用的地方有什么问题
function calc()
{quan_1= document.book.quan1.value;
quan_2= document.book.quan2.value;
quan_3= document.book.quan3.value;
quan_4= document.book.quan4.value;
quan_5= document.book.quan5.value;
quan_6= document.book.quan6.value;
quan_7= document.book.quan7.value;
unit_1= document.book.unit1.value;
unit_2= document.book.unit2.value;
unit_3= document.book.unit3.value;
unit_4= document.book.unit4.value;
unit_5= document.book.unit5.value;
unit_6= document.book.unit6.value;
unit_7= document.book.unit7.value;
total_1= document.book.total1.value;
total_2= document.book.total2.value;
total_3= document.book.total3.value;
total_4= document.book.total4.value;
total_5= document.book.total5.value;
total_6= document.book.total6.value;
total_7= document.book.total7.value;
totalPrice1= (unit_1 * quan_1)
document.book.total1.value=totalPrice1 .toFixed(2);
totalPrice2= (unit_2 * quan_2)
document.book.total2.value=totalPrice2 .toFixed(2);
totalPrice3= (unit_3 * quan_3)
document.book.total3.value=totalPrice3 .toFixed(2);
totalPrice4= (unit_4 * quan_4)
document.book.total4.value=totalPrice4 .toFixed(2);
totalPrice5= (unit_5 * quan_5)
document.book.total5.value=totalPrice5 .toFixed(2);
totalPrice6= (unit_6 * quan_6)
document.book.total6.value=totalPrice6 .toFixed(2);
totalPrice7= (unit_7 * quan_7)
document.book.total7.value=totalPrice7 .toFixed(2);
subTotal_= (totalPrice1 + totalPrice2 + totalPrice3 + totalPrice4 + totalPrice5 + totalPrice6 + totalPrice7);
document.book.subTotal.value= subTotal_ .toFixed(2);
taxValue= document.book.county.value;
calcTax= (subTotal_ * taxValue).toFixed(2)
document.book.tax.value= calcTax;
if (document.book.county.value == "0")
{alert("Please select a county");}
if (subTotal_ <= 15)
{shippingCost= "5.5"}
else {shippingCost= 5.5 + (0.0450 * subTotal_)}
document.book.shipping.value= shippingCost .toFixed(2);
grandTotal = (subTotal_ + shippingCost + calcTax );
document.book.grandtotal.value= grandTotal. toFixed(2);
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> </title>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<script src = "dynDate.js">
</script>
<script src = "beyond14.js">
</script>
<link rel = "stylesheet" href = "beyond14.css">
</head>
<body>
<script language="javascript" type="text/javascript">
document.write("<h6>Last Modified on: "+document.lastModified+"</h6>");
</script>
<main>
<center>
<table>
<form name="book" id="book">
<tr>
<th width="180px" > Cover </th>
<th width="270px"> Title </th>
<th width="200px"> Product Type </th>
<th width="200px"> Quantity </th>
<th width="230px"> Unit Price </th>
<th width="200px"> Total Price </th>
</tr>
<tr>
<td class="align"> <img src="bk_book_woman.jpg" width="120px" height="150px" name="bookwoman" title="bookwoman"> </td>
<td class="align"><input type="text" readonly="readonly" value="The Book Woman of Troublesome Creek" size="36"> </td>
<td class="align"> <select size="1" name="type1" id="type1">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan1" id="quan1"> </td>
<td class="align"> <input type="text" readonly="readonly" value="15.50" name="unit1" id="unit1"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total1" id="total1"> </td>
</tr>
<tr>
<td class="align"> <img src="au_dispatcher.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="The Dispatcher" size="36"> </td>
<td class="align"> <select size="1" name="type2" id="type2">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan2" id="quan2"> </td>
<td class="align"> <input type="text" readonly="readonly" value="12.50" name="unit2" id="unit2"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total2" id="total2"> </td>
</tr>
<tr>
<td class="align"> <img src="dk_gaia.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="Dreams of Gaia" size="36"> </td>
<td class="align"> <select size="1" name="type3" id="type3">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan3" id="quan3"> </td>
<td class="align"> <input type="text" readonly="readonly" value="24.99" name="unit3" id="unit3"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total3" id="total3"> </td>
</tr>
<tr> <td class="align"> <img src="bk_goldenInDeath.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="Golden in Death" size="36"> </td>
<td class="align"> <select size="1" name="type4" id="type4">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan4" id="quan4"> </td>
<td class="align"> <input type="text" readonly="readonly" value="22.50" name="unit4" id="unit4"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total4" id="total4"> </td>
</tr>
<tr> <td class="align"> <img src="bk_jefferson.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="A Walk Down Historic Jefferson" size="36"> </td>
<td class="align"> <select size="1" name="type5" id="type5">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan5" id="quan5"> </td>
<td class="align"> <input type="text" readonly="readonly" value="13.50" name="unit5" id="unit5"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total5" id="total5"> </td>
</tr>
<tr>
<td class="align"> <img src="puz_millennium.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="The New Millennium" size="36"> </td>
<td class="align"> <select size="1" name="type6" id="type6">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan6" id="quan6"> </td>
<td class="align"> <input type="text" readonly="readonly" value="18.95" name="unit6" id="unit6"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total6" id="total6"> </td>
</tr>
<tr>
<td class="align"> <img src="bk_signs.jpg" width="120px" height="150px" name="" title=""> </td>
<td class="align"><input type="text" readonly="readonly" value="Signs & Symbols Sourcebook" size="36"> </td>
<td class="align"> <select size="1" name="type7" id="type7">
<option> Audio </option>
<option> Deck </option>
<option> Fiction </option>
<option> Music </option>
<option> Non-Fiction </option>
<option> Puzzle </option>
<option> Reference </option>
</select> </td>
<td class="align"> <input type="number" size="3" name="quan7" id="quan7"> </td>
<td class="align"> <input type="text" readonly="readonly" value="34.95" name="unit7" id="unit7"> </td>
<td class="align"> <input type="text" readonly="readonly" name="total7" id="total7"> </td>
</tr>
<tr> <td>TN County: <select size="1" name="county" id="county">
<option value="0"> Select your county </option>
<option value="0.0925"> Anderson </option>
<option value="0.0875"> Cannon </option>
<option value="0.0950"> Fayette </option>
<option value="0.0800"> Johnson </option>
<option value="0.0975">Williamson </option>
</select>
<td colspan="3" class="alignR"> </td>
<td class="alignR"> SubTotal: </td>
<td class="align"> <input type="text" readonly="readonly" name="subTotal" id="subTotal"> </td> </tr>
<tr>
<td colspan="4"> </td>
<td class="alignR"> S&H: </td>
<td class="align"> <input type="text" name="shipping" id="shipping" readonly="readonly" </td> </tr>
<tr>
<td colspan="4"> </td>
<td class="alignR"> Tax: </td>
<td class="align"> <input type="text" name="tax" id="tax" readonly="readonly"> </td> </tr>
<tr>
<td colspan="4"> </td>
<td class="alignR"> Grand Total: </td>
<td class="align"> <input type="text" readonly="readonly" name="grandtotal" id="grandtotal"> </td> </tr>
<tr>
<td colspan="6" class="align"> <input type="button" class="calc" value="Calculate Order" onclick="calc()"> </tr>
<tr>
<td colspan="6" class="align"> <input type="reset" class="del" value="Cancel Order"> </tr>
</center>
</form>
</main>
</html>
答案 0 :(得分:0)
第一件事是删除函数调用和变量之间的所有空格,我希望这只是粘贴格式问题。
然后,您必须确保要对数字类型的值调用toFixed
,以便可以首先转换为数字,以确保未在字符串上使用它。
因此使其类似于:
Number(someVariable).toFixed()
答案 1 :(得分:0)
如果(subTotal_ <= 15) {shippingCost = “ 5.5” }
“ 5.5”是字符串,而不是数字。您正在尝试将其添加到两个数字,这会将所有内容更改为一个字符串,然后在该字符串上调用 toFixed()(仅适用于数字)。将“ 5.5” 更改为 5.5 !
答案 2 :(得分:0)
由于注释状态.toFixed()
是Number
类型的方法,因此您尝试在String
类型上使用它。因此,将您的数据转换为数字将解决该问题。最简单的方法是在字符串前面加上+
。这是一个示例:
var x = "5";
var y = "6";
console.log(x + y); // "56" because of string concatenation
console.log(+x + +y); // 11 because prepending a + converts string to number