javascript总和计算不起作用

时间:2012-05-15 21:05:30

标签: javascript html forms cgi

我有一份cgi表格供员工输入旅行费用。我正在使用javascript自动显示&发送计算数据。表格上的第一个(里程)计算有效。我无法获得第二次(总计算)计算。我正在寻找的结果是 - 里程*费率(0.555)=里程(工程) MileageExp +其他输入字段= TotalExpenses(不工作)

这是javascript -

<script type="text/javascript">
function totalexpense()
{
//mileage
a = document.event.Miles.value
b = document.event.Rate.value

c = a * b
z = c

document.event.Mileage.value = c.toFixed(2)

//total
d = document.event.Parking.value
e = document.event.Tolls.value
f = document.event.Plane.value
g = document.event.Train.value
h = document.event.Bus_Taxi.value
i = document.event.Lodging.value
j = document.event.Food.value
k = document.event.Other.value

l = z + d + e + f + g + h + i + j + k

document.event.Total_Expenses.value = l.toFixed(2)

}
</script>

浏览器显示document.event.Total_Expenses.value = l.toFixed(2)行中存在错误。

HTML -

<p align="left" class="bold"><span class="style8">Mileage:</span><br>
Miles:
          <input name="Miles" size="10"> 
     X rate: 
       <input name="Rate" size="10" value="0.555" onblur="totalexpense();">
= 
       <input name="Mileage" size="10" readonly=true>
</p>

<p align="left">Parking:</span><br>
            <input name="Parking" size="10" onblur="totalexpense();">
</p>

<p align="left">Tolls:</span><br>
            <input name="Tolls" size="10" onblur="totalexpense();">
</p>

<p align="left">Airline Tickets:</span><br>
            <input name="Plane" size="10" onblur="totalexpense();">
</p>

<p align="left">Train:</span><br>
            <input name="Train" size="10" onblur="totalexpense();" >
</p>

<p align="left">Bus/Taxi:</span><br>
            <input name="Bus_Taxi" size="10" onblur="totalexpense();" >
</p>

<p align="left">Lodging:</span><br>
            <input name="Lodging" size="10" onblur="totalexpense();" > 
            Accomodations with- <input name="Accomodations_With" type="text"    size="40" />
</p>

<p align="left" class="bold"><span class="style8">Food/Beverage:</span><br>
            <input name="Food" size="10" onblur="totalexpense();" >
</p>

<p align="left">Other Expenses:</span><br>
            <input name="Other" size="10" onblur="totalexpense();" >
</p>

    <p align="left" class="bold"><span class="style8">Other Expense Notes:</span><br>
                  <span class="main style22">
                  <textarea name="Other_Expense_Notes" cols="50" rows="3" class="fieldbkgrd"></textarea>
                </span>
</p>

<h3>Total Expenses:<br>
            <input name="Total_Expenses" size="10" READONLY>

3 个答案:

答案 0 :(得分:1)

您需要将值从字符串转换为数字 - .toFixed仅适用于数字。

document.event.Total_Expenses.value = Number(l).toFixed(2);

您的代码使用里程的原因是因为0.555被转换为以下整数:

c = a * b; (c = # * 0.555)

然后你做了“加法”,它被视为串联:

z + d + e + f + g + h + i + j + k;

这将int转换为字符串。因此:

document.event.Total_Expenses.value = l.toFixed(2);

失败,因为类型转换现在是一个字符串。

如果要显式转换值,则需要使用parseInt();

d = parseInt(document.event.Parking.value);

答案 1 :(得分:1)

您需要将值从字符串转换为数字 - .toFixed仅适用于数字。

document.event.Total_Expenses.value = Number(l).toFixed(2);

答案 2 :(得分:0)

<input name="Total_Expenses" size="10" READONLY>

您将其设置为只读,因此您无法将值设置为只读元素,这是我最好的猜测