mat106
06-15-2004, 03:14 PM
Hi.
I'm trying to create a form for users to submit a value in a text box and choose a unit from a drop down menu. On submitting, the script will then carry out certain conversions of the inputted value based on the unit that was chosen from the drop down menu and output the converted values. This is what i've got so far:
<html>
<head>
<script type="text/javascript">
function cal(form) {if (document.form1.unit1.selectedIndex = 0){
var i = document.form1.text1.value/3
var a = document.form1.text1.value/2
document.form1.answer.value = i
document.form1.answer1.value = a}
else if (document.form1.unit1.selectedIndex = 1)
{var i = document.form1.text1.value/2
var a = document.form1.text1.value/3
document.form1.answer.value = i
document.form1.answer1.value = a}
}
</script>
</head>
<body>
Input a value and choose calculate:
<form name="form1">
<input type="text" name="text1">
<select name="unit1">
<option selected>Joules</option>
<option>eV</option>
<option>Hartree</option>
</select>
<input name="button" type="button" onClick="cal(this.form)" value="Calculate">
<br>
<br>
Answers:<br>
<input type="text" name="answer">
Joules<br>
<input type="text" name="answer1">
eV
</form>
</body>
</html>
The problem is that i can't seem to get the script to respond to the option chosen from the drop down menu. It responds it all kinds of weird (wrong) ways. If think the problem is with the bold parts of the code. Can anyone help?
Thanks.
I'm trying to create a form for users to submit a value in a text box and choose a unit from a drop down menu. On submitting, the script will then carry out certain conversions of the inputted value based on the unit that was chosen from the drop down menu and output the converted values. This is what i've got so far:
<html>
<head>
<script type="text/javascript">
function cal(form) {if (document.form1.unit1.selectedIndex = 0){
var i = document.form1.text1.value/3
var a = document.form1.text1.value/2
document.form1.answer.value = i
document.form1.answer1.value = a}
else if (document.form1.unit1.selectedIndex = 1)
{var i = document.form1.text1.value/2
var a = document.form1.text1.value/3
document.form1.answer.value = i
document.form1.answer1.value = a}
}
</script>
</head>
<body>
Input a value and choose calculate:
<form name="form1">
<input type="text" name="text1">
<select name="unit1">
<option selected>Joules</option>
<option>eV</option>
<option>Hartree</option>
</select>
<input name="button" type="button" onClick="cal(this.form)" value="Calculate">
<br>
<br>
Answers:<br>
<input type="text" name="answer">
Joules<br>
<input type="text" name="answer1">
eV
</form>
</body>
</html>
The problem is that i can't seem to get the script to respond to the option chosen from the drop down menu. It responds it all kinds of weird (wrong) ways. If think the problem is with the bold parts of the code. Can anyone help?
Thanks.