PDA

View Full Version : Form with input and select-help!


Richard Welch
11-01-2006, 10:06 PM
Hi, I'm new to Javascript and so please excuse the below.

Basically I am trying to create a form with three variables; number of words, to be input by the user; target language, to be selected and text type, again to be selected. Source language is not included in the formula of the form but is selected anyway.

numberof words*target language+text type=total cost
eg:200*0.09+0.06=

I have attached what I atempted to do below. If anyone can give me directions in where I'm going wrong, it would be greatly appreciated.

Thanks,

Richard

<head>
<title>Translator calculator</title>
<script type="text/javascript">
<!--
var words = new Array(0); // number of words
var target = new Array(0.09,0.1); // target language
var text = new Array(0.06,0,0.02,0.02,0.04,0.03,0.01); // type of text

function Calc() {
var total = 0;
var obwords = document.getElementById('words');
var obtarget = document.getElementById('target');
var obtype = document.getElementById('type');

var w = obwords.Number[obwords.form1.words].value;
var ta = obtarget.options[obtarget.selectedIndex].value;
var ty = obtype.options[obtype.selectedIndex].value;

total = parseFloat(words[w])*parseFloat(target[ta])+parseFloat(type[ty]);
document.getElementById('Total').value = total.toFixed(2);
}
//-->
</script>
</head>


<body>
<form name="form1">
<p><font color="#BEB541" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Number
of Words:</strong></font><br>
<input TYPE="TEXT" SIZE="20" NAME="words">
</p>
<p><font color="#BEB541" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Source
Language:</strong></font><br>
<select id="source">
<option VALUE="" SELECTED>Select Language >
<option VALUE="1">English</option>
<option VALUE="1">Albanian</option>
<option VALUE="2">Arabic</option>
<option VALUE="2">Bengali</option>
<option VALUE="1">Bulgarian</option>
<option VALUE="2">Burmese</option>
<option VALUE="2">Chinese (simplified)</option>
<option VALUE="2">Chinese (traditional)</option>
<option VALUE="1">Croatian</option>
<option VALUE="1">Czech</option>
<option VALUE="1">Danish</option>
<option value="2">Dari</option>
<option value="1">Dutch</option>
<option value="1">Estonian</option>
<option value="2">Farsi</option>
<option value="1">Finnish</option>
<option value="1">French</option>
<option value="1">French Canadian</option>
<option value="1">German</option>
<option value="1">Greek</option>
</select>
</p>
<p><font color="#BEB541" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Target
Language:</strong></font><br>
<select id="target">
<option VALUE="" SELECTED>Select Language >
<option VALUE="1">English</option>
<option VALUE="1">Albanian</option>
<option VALUE="2">Arabic</option>
<option VALUE="2">Bengali</option>
<option VALUE="1">Bulgarian</option>
<option VALUE="2">Burmese</option>
<option VALUE="2">Chinese (simplified)</option>
<option VALUE="2">Chinese (traditional)</option>
<option VALUE="1">Croatian</option>
<option VALUE="1">Czech</option>
<option VALUE="1">Danish</option>
<option value="2">Dari</option>
<option value="1">Dutch</option>
<option value="1">Estonian</option>
<option value="2">Farsi</option>
<option value="1">Finnish</option>
<option value="1">French</option>
<option value="1">French Canadian</option>
<option value="1">German</option>
<option value="1">Greek</option>
<option value="2">Gujarati</option>
</select>
<p><font color="#BEB541" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Text
Type:</strong></font><br>
<select id="type">
<option VALUE="" SELECTED>Select Type >
<option VALUE="1">Legal</option>
<option VALUE="2">Business/Financial</option>
<option VALUE="3">Technical</option>
<option VALUE="4">Scientific</option>
<option VALUE="5">Medical</option>
<option VALUE="6">Software</option>
<option VALUE="7">Website</option>
</select>
<button onClick="Calc()">Calculate</button>
Total: <input id="Total" type="text" value="">
</center>
</form>
</body>
</html>

Kor
11-06-2006, 10:56 AM
w, ta and ty variables are strings, so that they can not be used as indexes of the arrays' elements. Make them numbers. And you may use directly the select's value, as it is always the same as the selected option's value.

var w = Number(obwords.value);
var ta = Number(obtarget.value);
var ty = Number(obtype.value);


And use rather Number() instead of parseFloat()