Javascript旅行计算器不工作

时间:2015-03-16 05:13:47

标签: javascript calculator

我试图制作一个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; 

}};

这张表是在正文中。将有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>

2 个答案:

答案 0 :(得分:0)

    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; 

}}

你在函数中添加了额外的大括号。此外,函数不会以半冒号结束。请检查更新的大括号

答案 1 :(得分:0)

我认为问题是脚本标记<script language="JavaScript/text">,看起来浏览器没有解析您的script元素。

<script language="javascript">

script的语言属性没有任何标准值,也不推荐使用,您也可以省略它。或者您可以使用type属性<script type="text/javascript">