...

View Full Version : Resolved Correctly passing a radio button value



indianagreg
01-05-2011, 03:14 PM
I'm a js noob but have managed to cobble together some code for a simple calculator. All the math works well, but changing the radio button doesn't have any effect. Basically, I have 4 set of equations (long if/then statements) - 2 are to be used when one radio button is checked, 2 if the other is checked.



<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

<script language="JavaScript">

function temp(form)
{
var r1 = form.r1.value;
var GNW = parseFloat(form.GNW.value, 10);
var BNW = parseFloat(form.BNW.value, 10);
var totalfee = 0;
var netfee = 0;
var discount = 0;


//if the "old" radio button is selected, use the the next two if/then formulas
if (r1=1)
{

for brevity sake, I'll omit the first two if/then equations



//close out the "old" options
}

//use the "new" option
else if (r1=2)
{
// two more if/then statements
}

// this applies to all conditions

discount = totalfee - netfee;

form.totalfee.value = totalfee/4;
form.netfee.value = netfee/4;
form.discount.value = discount/4;
}


and now the radio buttons


<input name="r1" value=1 type="radio" checked="checked">Old (3m/3m)
<input name="r1" value=2 type="radio" >Current (5m/5m)</p><p>


hopefully the brevity makes the answer apparent, the full code is attached if I've left out a crucial detail.

devnull69
01-05-2011, 03:50 PM
The compare operator is ==


if(r1==1) {
...
}

indianagreg
01-05-2011, 04:05 PM
devnull69,
Thanks for the quick reply. When I make that change, the whole script stops working (it just returns zeros).

If I set the value of the radio button to words vs. numbers, will that make a difference?



<input name="r1" value="old" type="radio" checked="checked">Old (3m/3m)
<input name="r1" value="new" type="radio" >Current (5m/5m)</p><p>


and then use


if(r1 == "old) {
...
}

indianagreg
01-05-2011, 08:12 PM
figured it out. used


if (form.r1[0].checked)

instead of the r1 value stuff.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum