PDA

View Full Version : Selecting a radio button to determine a value



assassin204
05-07-2010, 10:05 PM
Hi,

I am doing a project, and am need of some help with my code. My assignment is to basically make a program that calculates a household's carbon emissons, in doing this, I have to calculate whether the households cars are running Unleaded, Diesel or LPG fuel for their car...To do this i have made 3 radio buttons (Unleaded , Diesel, LPG). From this i want to be able to determine the amount of fuel used with either Unleaded , Diesel, LPG and then * it by either:

x2.8 Unleaded
x3 Diesel
x1.8 LPG
--------------------------------------------------------------------------
So thats pretty much where im stuck

If u can help, i'd be more than grateful.

Thanks,
assassin204

vwphillips
05-08-2010, 12:17 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function Cal(){
var frm=document.forms[0];
var miles=frm.miles.value=frm.miles.value.replace(/\D/g,'')||0;
var rads=frm.fuel;
frm.emissons.value='0';
for (var z0=0;z0<rads.length;z0++){
if (rads[z0].checked&&miles){
frm.emissons.value=(miles*rads[z0].value).toFixed(2);
return true;
}
}
alert('please select fuel type\nand enter milage');
return false;
}
/*]]>*/
</script></head>

<body>
<form>
x2.8 Unleaded <input type="radio" name="fuel" value="2.8" />
x3 Diesel <input type="radio" name="fuel" value="3" />
x1.8 LPG <input type="radio" name="fuel" value="1.8" />
<br />
Miles <input name="miles" /><br />
Emissons <input name="emissons" /><br />
<input type="button" name="" value="Calculate" onclick="Cal();"/>
</form>

</body>

</html>