Click to See Complete Forum and Search --> : SOS!!! same code:radio button - works. select optionn - crashed


Natasha
12-04-2003, 04:49 AM
Hi, all!

need abit of help. please. Created a small calculator, which works excellent when customer chooses offered options. clicking radio button. Changed those to drop down menu - all mathematical result - :( - nonsence.
Code's skeleton is absolutely the same.....


COMMON PART - calculation formula
---------------------------------------
<script language="" type="text/javascript">
function calculate(obj) {
var w = (obj.wu[1].checked) ? obj.wt.value : obj.wt.value * 6.35029318
var h = (obj.hu[1].checked) ? obj.ht.value : obj.ht.value * 0.30480
obj.calcval.value = Math.round(w / h / h * 10) / 10
}
</script>
--------------------------------------

radio button option:


<form name="bmi" action=" ">
<p><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>&nbsp;&nbsp;Height:&nbsp;</strong></font>
<input type="text" name="ht" size="8" />
<input type="radio" name="hu" value="i" checked="checked" />
<font color="#666666" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>feet.inch</b> <br>
<img src="pics/spacer.gif" width="144" height="1" border="0">
<input type="radio" name="hu" value="m" /><b>&nbsp;&nbsp;meters.centimeters</b></font><br>
<font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>&nbsp;&nbsp;Weight:</strong></font>
<input type="text" name="wt" size="8" />
<input type="radio" name="wu" value="l" checked="checked" />
<font color="#666666" size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>stones.pounds</b><br>
<img src="pics/spacer.gif" width="144" height="1" border="0">
<input type="radio" name="wu" value="k" /><b>&nbsp; kg.grams</b></font></p>

<p>
<input type="hidden" name="calcval" size="10">
<img src="pics/spacer.gif" width="200" height="1" border="0">
<input name="button2" type="button" class="result" onClick="calculate(this.form), newWin()" value=" " />

</p>

</form>
------------------------------------

DROP DOWN MENU option:

<form name="bmi" action=" ">

<table width="332" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign="top">
<td><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Height:&nbsp;</strong></font>
<input type="text" name="ht" size="8" /></td>
<td><select name="hu">
<option value="i" selected>Feet &amp; Inches
<option value="m">Meters &amp; cm
</select>
</td>
</tr>
<tr valign="top">
<td colspan="2"><font color="#666666" size="1" face="Verdana, Arial, Helvetica, sans-serif">i.e.
6.8</font></td>
</tr>
<tr valign="top">
<td><font color="#666666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Weight:</strong></font>
<input type="text" name="wt" size="8" /></td>
<td><select name="wu">
<option value="l" selected>Stones &amp; Pounds
<option value="k">Kg &amp; Grams
</select>
<font color="#666666" size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
</tr>
<tr valign="top">
<td colspan="2"><font color="#666666" size="1" face="Verdana, Arial, Helvetica, sans-serif">i.e.
13.5
</font></td>
</tr>
<tr>
<td><input type="hidden" name="calcval" size="10"></td>
<td><input name="button2" type="button" class="result" onClick="calculate(this.form), newWin()" value=" submit" /></td>
</tr>
</table>

</p>
<img src="pics/spacer.gif" width="200" height="1" border="0">
</form>


THANK YOU IN ADVANCE!!!!!!

Pittimann
12-04-2003, 05:59 AM
Hi!

An option of a select is not "checked", but "selected".

My proposal: two different functions for the two forms.

function calculate(obj) {// for radio
var w = (obj.wu[1].checked) ? obj.wt.value : obj.wt.value * 6.35029318
var h = (obj.hu[1].checked) ? obj.ht.value : obj.ht.value * 0.30480
obj.calcval.value = Math.round(w / h / h * 10) / 10
}
function calculate2(obj) {//for dropdown
var w = (obj.wu[1].selected) ? obj.wt.value : obj.wt.value * 6.35029318
var h = (obj.hu[1].selected) ? obj.ht.value : obj.ht.value * 0.30480
obj.calcval.value = Math.round(w / h / h * 10) / 10
}

Then change the code for the button of your form with the select stuff:
<input name="button2" type="button" class="result" onClick="calculate2(this.form)" value="submit"></td>
Should work like that...

Cheers - Pit

Natasha
12-04-2003, 07:04 AM
Yahooo!!!!!

Pittimann
12-04-2003, 07:11 AM
You're welcome!!! :)