目标是获取每种类型的订单数,并在单击总成本时计算所有订单的总价。但是我似乎无法让它工作谷歌调试器说
Uncaught SyntaxError: Unexpected token ; 13
Uncaught ReferenceError: compute is not defined
编辑:修复了括号,但是现在我在返回lin的输出lin上遇到了问题。
Edit2:添加了.value以返回
[解决了]
<head>
<title>Coffee Order Form</title>
<script type="text/javascript">
function compute()
{
//input
var french = Number(document.getElementById("french").value;
if(isNaN(french))
{
french=0;
}
var hazelnut = Number(document.getElementById("hazelnut").value);
if(isNaN(hazelnut))
{
hazelnut=0;
}
var columbian = Number(document.getElementById("columbian").value);
if(isNaN(columbian))
{
columbian=0;
}
//calculate
var total = 3.49 * french + 3.95 * hazelnut + 4.59 * columbian;
//output
return document.getElementById("total").value = "$" + total.toFixed(2);
}
</script>
</head>
<body>
<form method="post" action="http://uta.edu">
<table border="2">
<caption><b>Coffee Order Form</b></caption>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<tr>
<th>French Vanilla</th>
<th>$3.49</th>
<td><input type="text" id="french"></td>
</tr>
<tr>
<th>Hazelnut Cream</th>
<th>$3.95</th>
<td><input type="text" id="hazelnut"></td>
</tr>
<tr>
<th>Columbian</th>
<th>$4.59</th>
<td><input type="text" id="columbian"></td>
</tr>
</table>
<input type="button" value="Total Cost" onclick="compute()">
<input type="text" id="total">
<br/>
<input type="submit" value="Submit Order">
<input type="reset" value="Clear Order Form">
</form>
</body>
最佳答案
您缺少对Number()的所有3个调用的结尾括号:
var french = Number(document.getElementById("french").value;
应该:
var french = Number(document.getElementById("french").value);
由于语法错误,该函数对主体中的代码不可用。因此,未捕获的参考误差。
关于javascript - 只需Javascript即可汇总表单字段,我的问题在哪里?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22213623/