...

View Full Version : Calcultate Amount Total according to dropdown



Mozzi
07-09-2011, 02:03 PM
I have a text field.
I want the text field to change / be calculated on the action made on the dropdown menu...

So if I select Debit card.. it should leave the amount as is..
If I selected Credit Card it should add a percentage to the Amount.

Please dont just provide a solutoin.. but please explain as i would love to understand why




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">

function doMath() {

if (CardType = "cc") {
var one = eval(document.myForm.Amount.value)
var prod = one * 1.03
document.myForm.Amount.value=custRound(prod,2);}
}

else
document.myForm.Amount.value;

function custRound(x,places) {
return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}
</script>


</head>

<body>
<form id="myForm" name="myForm" method="post" action="">
<input name="Amount" type="text" id="Amount" v alue="100" />
<select id="CardType" name="CardType" onchange="doMath();" >
<option value="" selected="selected">Please Select</option>
<option value="dc">Debit Card</option>
<option value="cc">Credit Card</option>

</select>
</form>
</body>
</html>

jmrker
07-10-2011, 05:09 AM
You have several syntax errors. If you use FF, look at the 'error console' contents in the "Tools" menu
For example: Select box value="100" not v alue="100"
and '=' is an assignment command while '==' is a comparison command for the "if" statement

I put several comments to other fixes into your code.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">

function doMath(info) {
total = document.myForm.Amount.value;
if (info == "cc") { // comparison is '==' NOT '=' (that is assignment)
var prod = total * 1.03;
document.myForm.Amount.value=custRound(prod);
}
}
function custRound(x) {
// function custRound(x,places) {
// awful lot of work to round a number
// return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
// when you can simply call the toFixed() function as in
return x.toFixed(2);
}
</script>


</head>

<body>
<form id="myForm" name="myForm" method="post" action="">
<input name="Amount" type="text" id="Amount" value="100" />
<select id="CardType" name="CardType" onchange="doMath(this.value)" >
<option value="" selected="selected">Please Select</option>
<option value="dc">Debit Card</option>
<option value="cc">Credit Card</option>
</select>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum