PDA

View Full Version : My Brain is Blank - Simple calculate form.

justme2
09-19-2005, 10:25 PM
I've been staring at this computer way too long to figure this simple task out. I don't want the calculated total on this form to appear in a pop up alert window. I want a text box next to the calculate button where the figure appears.

I know, I know!!!!! Don't quit my day job right?? :o

<html>
<title>Calculator</title>
<script type="text/javascript">
var disc = [];
//... Example of discount code-----disc["i13_i23_i33"] = 60;---//
//no TV service selected
//i1
disc["dv1_tv0_i1"] = 39.91;

disc["dv2_tv0_i1"] = 49.11;

function calculate(Form){
var total = 0;
if (dv && pvtv && net){ //all checked
var d = disc[dv.id + "_" +pvtv.id + "_" + net.id];
if (typeof d != "undefined"){ //discount exists in the array
total = d;
}
else{ //no discount
total = Number(dv.value) + Number(pvtv.value) + Number(net.value);
}
}
else {
return;
}
}

for (var i=0; i<len; i++){
}
return null;
}

//from liorean (http://www.codingforums.com/showthr...8077#post178077)
Number.prototype.toDecimals=function(n){
n=(isNaN(n))?
2:
n;
var
nT=Math.pow(10,n);
s=s||'.';
return (s.length>n)?
s:
}
return (isNaN(this))?
this:
(new String(
Math.round(this*nT)/nT
}
</script>
<body>
<form>
<fieldset>
<legend>TService </legend>
<input name="dv" type="radio" id="dv0" value="0" checked />
None
<label for="dv0"> </label>
<br />
<input type="radio" name="dv" id="dv1" value="12.75" />
Line
<label for="dv1"> - \$12.75</label>
<br />
<input type="radio" name="dv" id="dv2" value="21.95" />
<label for="dv2">Basic - \$21.95</label>
<br />
<input type="radio" name="dv" id="dv3" value="34.95" />
<label for="dv3">Unlimited - \$34.95<br>
<input type="radio" name="dv" id="dv4" value="47.50" />
Unlimited Plus- \$47.50</label>
<br />
</fieldset>
<fieldset>
<legend>Services </legend>
<input name="pvtv" type="radio" id="tv0" value="0" checked />
<label for="tv0">None</label><br>
<input type="radio" name="pvtv" id="tv1" value="15.17" />
<label for="tv1">Connector - \$15.17</label><br>
<input type="radio" name="pvtv" id="tv2" value="33.79" />
<label for="tv2">Plus - \$33.79</label><br>
<input type="radio" name="pvtv" id="tv3" value="39.95" />
<label for="tv3"> Digital- \$39.95</label><br>

</fieldset>
<fieldset>
<legend>IServices</legend>
<input name="net" type="radio" id="i0" value="0" checked />
<label for="i0">None</label><br>
<input type="radio" name="net" id="i1" value="31.95" />
<label for="i1">Modem - \$31.95</label><br>
<input type="radio" name="net" id="i2" value="34.95" />
<label for="i2">Modem- \$34.95</label><br>
<input type="radio" name="net" id="i3" value="39.95" />
<label for="i3">Modem- \$39.95</label><br>
</fieldset>
<input name="" type="button" onclick="calculate(this.form)" value="Calculate" />
</form>
</body>
</html>

TheShaner
09-19-2005, 10:50 PM
Add this input area right after you Calculate button:

<input type="text" name="TotalAmount" size="10" value="" />

Change your Calculate function to the below:

function calculate(Form){
var total = 0;
if (dv && pvtv && net){ //all checked
var d = disc[dv.id + "_" +pvtv.id + "_" + net.id];
if (typeof d != "undefined"){ //discount exists in the array
total = d;
}
else{ //no discount
total = Number(dv.value) + Number(pvtv.value) + Number(net.value);
}
Form.TotalAmount.value = "\$" + total.toDecimals(2);
}
else {
return;
}
}

The code in red does the trick. You can change the input box to the size you want. I just threw in a number.

-Shane

glenngv
09-20-2005, 06:43 AM
Javascript is case-sensitive. It should be value not Value.

Form.TotalAmount.value = "\$" + total.toDecimals(2);