Javascript 旅行计算器不工作

Javascript Travel Calculator not working

我正在尝试制作一个 JavaScript 计算器来计算 4 个家庭乘火车旅行的费用。一张成人票价为 32 美元,儿童票价为 20 美元,另加 10 美元佣金。这是我的JavaScript。当我 运行 在 Firefox 调试器中显示 amountdue1 未定义?

<script language="JavaScript/text">
var child = 20;
var adult = 32;

function amountdue1() {
{
if (document.calc.familyname1.value === null || document.calc.familyname1.value.length === 0) 
{
alert("Please enter your family name");
}
else
{


var numadults = document.calc.num_Adults1.selectedIndex;
var numchildren = document.calc.num_Children1.selectedIndex;

document.calc.totaldue.value = (numadults * adult) + (numchildren * child) + 10; 

}};

这个table在体内。将有 4 个到期金额,但我无法让一个工作。

<table width=100% border=1 class="one" id="table_one">
<br>
<tr>
<th width=20%>Family One</th>
<th width=20%>Family Two</th>
<th width=20%>Family Three</th>
<th width=20%>Family Four</th>
</tr>
<tr>
<td align=center>
<br>
<input type="text" name="familyname1" maxlength="20"><br>
</td>
<td align=center>
<br>
<input type="text" name="familyname2" maxlength="20"><br>
</td>
<td align=center>
<br>
<input type="text" name="familyname3" maxlength="20">
</td>
<td align=center>
<br>
<input type="text" name="familyname4" maxlength="20"><br>
</tr>
<tr>            
<td align=center><br>
<select id="num_Adults1" onchange="return total1()">
<option value=0 selected>0 Adults</option>
<option value=1>1 Adult</option>
<option value=1>2 Adults</option>
<option value=1>3 Adults</option>
<option value=1>4 Adults</option>
<option value=1>5 Adults</option>
<option value=1>6 Adults</option>
<option value=1>7 Adults</option>
<option value=1>8 Adults</option>
</select>

</td>
<td align=center><br>
<select id="num_Adults2" onchange="return total2()">
<option value=0 selected>0 Adults</option>
<option value=1>1 Adult</option>
<option value=1>2 Adults</option>
<option value=1>3 Adults</option>
<option value=1>4 Adults</option>
<option value=1>5 Adults</option>
<option value=1>6 Adults</option>
<option value=1>7 Adults</option>
 <option value=1>8 Adults</option>
</select>
</td>
<td align=center><br>
<select id="num_Adults3" onchange="return total3()">
<option value=0 selected>0 Adults</option>
<option value=1>1 Adult</option>
<option value=1>2 Adults</option>
<option value=1>3 Adults</option>
<option value=1>4 Adults</option>
<option value=1>5 Adults</option>
<option value=1>6 Adults</option>
<option value=1>7 Adults</option>
<option value=1>8 Adults</option>
</select>
</td>
<td align=center><br>
<select id="num_Adults4" onchange="return total4()">
<option value=0 selected>0 Adults</option>
<option value=1>1 Adult</option>
<option value=1>2 Adults</option>
<option value=1>3 Adults</option>
<option value=1>4 Adults</option>
<option value=1>5 Adults</option>
<option value=1>6 Adults</option>
<option value=1>7 Adults</option>
<option value=1>8 Adults</option>
</select>
</td>
</tr>
<tr>
<td align=center><br>
<select id="num_Children1" onchange="return total1()">
<option value=0 selected>0 Children</option>
<option value=1>1 Child</option>
<option value=1>2 Children</option>
<option value=1>3 Children</option>
<option value=1>4 Children</option>
<option value=1>5 Children</option>
<option value=1>6 Children</option>
<option value=1>7 Children</option>
<option value=1>8 Children</option>
</select>
</td>               
<td align=center>
<br>
<select id="num_Children2" onchange="return total2()">
<option value=0 selected>0 Children</option>
<option value=1>1 Child</option>
<option value=1>2 Children</option>
<option value=1>3 Children</option>
<option value=1>4 Children</option>
<option value=1>5 Children</option>
<option value=1>6 Children</option>
<option value=1>7 Children</option>
<option value=1>8 Children</option>
</select>
</td>
<td align=center><br>
<select id="num_Children3" onchange="return total3()">
<option value=0 selected>0 Children</option>
<option value=1>1 Child</option>
<option value=1>2 Children</option>
<option value=1>3 Children</option>
<option value=1>4 Children</option>
<option value=1>5 Children</option>
<option value=1>6 Children</option>
<option value=1>7 Children</option>
<option value=1>8 Children</option>
</select>
</td>
<td align=center><br>
<select id="num_Children4" onchange="return total4()">
<option value=0 selected>0 Children</option>
<option value=1>1 Child</option>
<option value=1>2 Children</option>
<option value=1>3 Children</option>
<option value=1>4 Children</option>
<option value=1>5 Children</option>
<option value=1>6 Children</option>
<option value=1>7 Children</option>
<option value=1>8 Children</option>
</select>
</td>
</tr>
<tr>
<td align=center>
<input type=button onclick="amountdue1()" name="total1" value="Find total">
<input type=reset value=Reset>
<br><br>
<input type="text" name="totaldue" size=15 readonly title="Total Amount" maxlength="9" placeholder="$">
</td>
<td align=center>
<input type=button onclick="total2()" value="Find total">
<input type=reset value=Reset>
<br><br>
<input type="text" name="totaldue2" size=15 readonly title="Total Amount" maxlength="9" placeholder="$">
</td>
<td align=center>
<input type=button onclick="total3()" value="Find total">
<input type=reset value=Reset>
<br><br>
<input type="text" name="totaldue3" size=15 readonly title="Total Amount" maxlength="9" placeholder="$">
</td>
<td align=center>
<input type=button onclick="total4()" value="Find total">
<input type=reset value=Reset>
<br><br>
<input type="text" name="totaldue4" size=15 readonly title="Total Amount" maxlength="9" placeholder="$">
</td>
</tr>
</table>
    function amountdue1() 
{
if (document.calc.familyname1.value === null || document.calc.familyname1.value.length === 0) 
{
alert("Please enter your family name");
}
else
{


var numadults = document.calc.num_Adults1.selectedIndex;
var numchildren = document.calc.num_Children1.selectedIndex;

document.calc.totaldue.value = (numadults * adult) + (numchildren * child) + 10; 

}}

您在 function.Also 中添加了额外的花括号,该函数不会以半结束 colon.Please 检查更新的花括号

我认为问题出在脚本标签 <script language="JavaScript/text">,浏览器似乎没有解析您的 script 元素。

<script language="javascript">

script 的语言属性没有任何标准值,它已被弃用,您也可以简单地省略它。或者您可以使用 type 属性 <script type="text/javascript">