gazhayes
Technical User
- Apr 15, 2008
- 1
Hi all,
I don't want to be lazy and ask someone to just try to find out whats wrong with my code, but I've been working on this for about an hour and cant make it work. I dont have any js books and anyway I'm more of a learn by doing kind of guy, actually today is the first time i used js and I have adapted this script to what I want but argh....
If anyone can give me some pointers that would be great! thanks
Here is the code that isn't working:
And here is the html im using:
I don't want to be lazy and ask someone to just try to find out whats wrong with my code, but I've been working on this for about an hour and cant make it work. I dont have any js books and anyway I'm more of a learn by doing kind of guy, actually today is the first time i used js and I have adapted this script to what I want but argh....
If anyone can give me some pointers that would be great! thanks
Here is the code that isn't working:
Code:
function processForm() {
//get form variables
//var theItems = document.getElementsByName( "item" );
var thePgs = Number( document.getElementById( "pgs" ).value );
var theChr = Number( document.getElementById( "chr" ).value );
var theFld = document.getElementsByName( "fld" );
var theTbl = Number( document.getElementById( "tbl" ).value );
var theTme = document.getElementsByName( "tme" );
var theFrm = document.getElementsByName( "frm" );
var theCur = document.getElementsByName( "cur" );
//var theTbl = document.getElementById( "tbl").checked;
//variable for selected item
var selectedFld;
var selectedTme;
var selectedFrm;
var selectedCur;
//variable for order total
var orderTotal = 0.00;
//the display box
var outBox = document.getElementById( "total" );
//select boxes
for(var i = 0; i < theFld.length; i++) {
if( theFld[i].checked) {
selectedFld = theFld[i].value;
break;
}
for(var i = 0; i < theTme.length; i++) {
if( theTme[i].checked) {
selectedTme = theTme[i].value;
break;
}
for(var i = 0; i < theFrm.length; i++) {
if( theFrm[i].checked) {
selectedFrm = theFrm[i].value;
break;
}
for(var i = 0; i < theCur.length; i++) {
if( theCur[i].checked) {
selectedCur = theCur[i].value;
break;
}
}
//Field
switch(selectedFld) {
case "A":
total = 1.0;
break;B
case "":
total = 1.1;
break;
case "C":
total = 1.2;
break;
default:
total = 0.00;
}
//Time
switch(selectedTme) {
case "A":
total = 1.0;
break;B
case "":
total = 1.1;
break;
case "C":
total = 1.2;
break;
default:
total = 0.00;
}
//Format
switch(selectedFrm) {
case "A":
total = 1.0;
break;B
case "":
total = 1.1;
break;
case "C":
total = 1.2;
break;
default:
total = 0.00;
}
//Currency
switch(selectedCur) {
case "A":
total = 1.0;
break;B
case "":
total = 1.1;
break;
case "C":
total = 1.2;
break;
default:
total = 0.00;
}
//figure final total
total = (((thePgs * theChr * theFld) + theTbl) * theTme * theFrm) * theCur;
//output to text box
outBox.value = "$" + total.toString();
And here is the html im using:
Code:
<form id="form1" action="" method="get">
Number of pages<input type="text" id="pgs" size="3" maxlength="3" value="1" />
Characters per page<input type="text" id="chr" size="3" maxlength="3" value="1" />
<input type="radio" name="fld" value="A" />Mining
<input type="radio" name="fld" value="B" />Geology
<input type="radio" name="fld" value="C" />Finance
<input type="radio" name="tme" value="A" />24 Hours
<input type="radio" name="tme" value="B" />3 Days
<input type="radio" name="tme" value="C" />1 Week
<input type="radio" name="frm" value="A" />Word Document
<input type="radio" name="frm" value="B" />PDF
<input type="radio" name="frm" value="C" />Image or Fax
<input type="radio" name="cur" value="A" />AUD
<input type="radio" name="cur" value="B" />RMB
<input type="radio" name="cur" value="C" />HKD
How many tables or diagrams?<input type="text" id="tbl" size="3" maxlength="3" value="1" />
Your total: <input type="text" id="total" size="10" maxlength="10" readonly="readonly" value="0" />
<input type="button" id="submit" value="Calculate Estimate cost" onclick="processForm()" />
</form>