...

View Full Version : Multiplication Error - OnBlur



ccwebstuff
01-17-2004, 04:39 PM
it's giving me a value of $150 for 3 of the western -- why is the math wrong ?

<script language="javascript">
<!--
function add( qty ){
//-->if more than 1 (western+english) - shipping is 0
//-->1=89, 2=89, > 2 & < 10 = 75, > 9 = 70

var nQty = eval( document.EzUpOrderForm.westernqty.value + document.EzUpOrderForm.englishqty.value )

switch (nQty) {
case nQty = 1 :
document.EzUpOrderForm.shipping.value = 11.00
document.EzUpOrderForm.cost.value = 89.00
break;

case nQty = 2 :
document.EzUpOrderForm.shipping.value = " * FREE * "
document.EzUpOrderForm.cost.value = eval( 89.00 * 2 )
break;
case nQty > 9 :
document.EzUpOrderForm.shipping.value = " * FREE * "
document.EzUpOrderForm.cost.value = eval( 70.00 * nQty )
break;
default:
document.EzUpOrderForm.shipping.value = " * FREE * "
document.EzUpOrderForm.cost.value = eval( 75.00 * nQty )
break;
}
}

// -->
</script>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" colspan="4"><font color="#C4816A" face="Arial, Helvetica, sans-serif" size="4"><b>Choose the Quantity of each style that you would like</b></font></td>
</tr>
<tr>
<td width="20%"><font face="Arial" color=#6D69C2 size="4"><b>Western Style</b></font></td>
<td width="31%"><font face="Arial" color=#C4816A size="4"><b>
<input type="text" name="westernqty" size="15" value="0" onblur="add(this);" >
</b></font></td>
<td width="29%"><font face="Arial" color=#6D69C2 size="4"><b>English Style</b></font></td>
<td width="20%"><font face="Arial" color=#C4816A size="4"><b>
<input type="text" name="englishqty" size="15" value="0" onblur="add(this);">
</b></font></td>
</tr>
</table>

Mr J
01-17-2004, 06:15 PM
You could try changing

var nQty = eval( document.EzUpOrderForm.westernqty.value + document.EzUpOrderForm.englishqty.value )

to

var nQty = eval( document.EzUpOrderForm.westernqty.value*1 + document.EzUpOrderForm.englishqty.value*1 )


Would have to see the rest of the script otherwise

glenngv
01-19-2004, 09:31 AM
eval() is unnecessary. If you search eval in this forum, you will find that eval==evil.

var nQty = parseFloat( document.EzUpOrderForm.westernqty.value) + parseFloat(document.EzUpOrderForm.englishqty.value);

to make it more efficient and to save typing, put your form reference in a variable

var f = document.EzUpOrderForm;

then in your codes:

var nQty = parseFloat(f.westernqty.value) + parseFloat(f.englishqty.value);


or instead, pass the form reference to the add() function.

function add(f){
var nQty = parseFloat(f.westernqty.value) + parseFloat(f.englishqty.value);
...
}
...
<input type="text" name="westernqty" size="15" value="0" onblur="add(this.form);" >
<input type="text" name="englishqty" size="15" value="0" onblur="add(this.form);">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum