Hi
I’ve got a table that needs to calculate the total from a combination of values generated from dropdowns and free text fields.
This is working okay but I’m getting the dreaded “NaN” in the total field until I enter a value in that text field and move off it.
Also I want to right align the values, display them all as currency to two decimal places.
Any help is appreciated
Cheers
Luigi
[code=php]
<html>
<head>
<title>computer</title>
<script type=”text/javascript”>
function calcTotal()
{
document.f.grandTotal.value =
parseInt(document.f.totComputer.value) +
parseInt(document.f.totDesk.value) +
parseInt(document.f.totChair.value) +
parseInt(document.f.other.value);
}
function setValue(name,src)
{
document.f[name].value = src.value;
calcTotal();
}
</script>
</head>
<body onload=”setValue(‘totComputer’,document.f.computer); setValue(‘totDesk’,document.f.Desk); setValue(‘totChair’,document.f.Chair); setValue(‘totChair’,document.f.other);”>
<form name=f method=”POST” action=”–WEBBOT-SELF–“>
<div align=”left”>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”307″>
<tr>
<td width=”71″>computer</td>
<td width=”161″><select size=”1″ name=”computer” onchange=”setValue(‘totComputer’,this);”>
<option value=1000>small</option>
<option value=2000>medium</option>
<option value=3000>large</option>
</select></td>
<td width=”196″><input type=”text” name=”totComputer” size=”20″></td>
</tr>
<tr>
<td width=”71″>desk</td>
<td width=”161″><select size=”1″ name=”Desk” onchange=”setValue(‘totDesk’,this);”>
<option value=100>wood</option>
<option value=200>glass top</option>
<option value=300>4 drawer</option>
</select></td>
<td width=”196″><input type=”text” name=”totDesk” size=”20″></td>
</tr>
<tr>
<td width=”71″>chair</td>
<td width=”161″><select size=”1″ name=”Chair” onchange=”setValue(‘totChair’,this);”>
<option value=80>highback</option>
<option value=40>lowrise</option>
</select></td>
<td width=”196″><input type=”text” name=”totChair” size=”20″></td>
</tr>
<tr>
<td width=”71″>other</td>
<td width=”161″>
</td>
<td width=”196″><input type=”text” name=”other” onBlur=”setValue(‘other’,this);” size=”20″></td>
</tr>
<tr>
<td width=”71″></td>
<td width=”161″>
<p align=”left”>total:</td>
<td width=”196″><input type=”text” name=”grandTotal” size=”20″></td>
</tr>
</table>
</div>
<p><input type=”submit” value=”Submit” name=”B1″><input type=”reset” value=”Reset” name=”B2″></p>
</form>
</body>
</html>