mr eyeglasses mr eyeglasses - 3 months ago 9
jQuery Question

How to pass select option to an input submit button

Say I have:

<form>
<select>
<option value="page1.html">Page 1</option>
<option value="page2.html">Page 2</option>
</select>
<input type="submit" value="Submit">
</form>


How do I make it so that when I select an option, I click on this Submit button and it goes to the appropriate link that was selected?

Answer

You can use this code:

$(function() {
  $('#frm1').on('submit', function() {
    window.location = $('#sel1').val();
    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="frm1">
 <select id="sel1"> 
   <option value="page1.html">Page 1</option>
   <option value="page2.html">Page 2</option>
  </select>
 <input type="submit" value="Submit">
</form>

Note that in the example it will not works, since there are no such pages on stackoverflow's server

Comments