Results 1 to 2 of 2

Thread: Selecting a radio button to determine a value

  1. #1
    Join Date
    May 2010
    Location
    Adelaide, Australia
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Selecting a radio button to determine a value

    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

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •