Help on a search form based on radio button selection, please

Discussion in 'JavaScript' started by hankliu, Mar 29, 2007.

  1. #1
    I am kinda new to javascript and I would like to make a search form with a text input field and two radio buttons, i.e, button A and button B, with A auto checked. If A is checked, a search will redirect to the page: http://www.urlA.com/SEARCH_TEXT , and if B is checked, after submitting the form it redirects to http://www.urlB.com/SEARCH_TEXT. Below is my code but it is not working, I guess there might be a problem on finding the value of the radio button:

    <script type="text/javascript">
    
    function get_radio_value()
    {
    for (var i=0; i < document.form1.R1.length; i++)
       {
       if (document.form1.R1[i].checked)
          {
          return document.form1.R1[i].value;
          }
       }
    }
    
    
    function getURL(val){
    
    radio_value = get_radio_value();
    
    if (radio_value==A)
    {base = 'http://www.domainA.com/';}
    else
    {base = 'http://www.domainB.com/';}
       location = base + val;
       return false;
    }
    </script>
    </head>
    
    <form id="form1" name="form1" method="post" action="" onsubmit="return getURL(this.url.value)">
      <label>
      <input type="text" name="url" size="20" /><input type="radio" name="R1" value="A" checked>A<input type="radio" name="R1" value="B">B
      <input type="submit" name="Submit" value="Submit" />
      </label>
      </form>
    
    Code (markup):
    Any help on the codes or suggestion of a new way to do this would be highly appreciated. Thanks in advance!
     
    hankliu, Mar 29, 2007 IP