I have the following field in a jsp page..

   <select size="1" name="typ">
                                    <option value="C">Coffe</option>
                                    <option value="T">Tea</option>
                                    &nbsp;
                                </select>

Now when the user selects Coffeand and clicks submit, I want the page to go to Coffe.jsp
and when Tea is selected , i want it to go to Tea.jsp

I tried using the following jquery code but it is not working...

 jQuery(document).ready(function() {
                jQuery("#typ").change(function() {
                    if(this.options[this.selectedIndex].value == "C") {
                        window.location = "/Coffee.jsp";
                    }
                });

I am a newbie in jquery script and jsp...

Recommended Answers

You can call javascript method on onchange() event of select and you can work on your logic to redirect.Something like this:-

javascript method

function redirect_url(url )
{
  alert(url);
}

select tag

 <select size="1" name="typ" onchange="redirect_url(this.value)">
                                    <option value="C">Coffe</option>
                                    <option value="T">Tea</option>
                                    &nbsp;
                                </select>
Jump to Post

All 3 Replies

You can call javascript method on onchange() event of select and you can work on your logic to redirect.Something like this:-

javascript method

function redirect_url(url )
{
  alert(url);
}

select tag

 <select size="1" name="typ" onchange="redirect_url(this.value)">
                                    <option value="C">Coffe</option>
                                    <option value="T">Tea</option>
                                    &nbsp;
                                </select>

you just have to use javascript for that as

<script type="text/javascript">
        $(function(){
           $('id').bind('change', function () {
             var url = $(this).val(); // get selected value
             alert(url); // This will help to check whether JS is getting executed.
             if (url) { // require a URL
                window.location = url; // redirect
             }
             return false;
           });
       });
</script>

you have to give id as you have given in the select tag.add this js file to your project.
else

you can write one more function i.e. on change event

function useme() {
  url=document.entry.Country.value
  if (url=="UK")
  {
  /*here i want to call the page response.jsp */
 alert('complete');
document.entry.action="response.jsp";
document.entry.subnit();

  }
  else

      {
      /*here i want to call the page submit.jsp */
         alert('now')
      }
  }

rgreg5greghtr5hgr5hgrt5hg

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.