Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a web form submit itself automatically up on a dropdown selection

I have a form in my page like the one below;

<form name="testform" id="testform" action="test.php" method="get">
  <input name="field1" id="field1" type="text" value="">
  <input name="field2" id="field2" type="text" value="">
  <select name="dropdown" id="dropdown">
    <option value="option1" selected="selected">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
  </select>
  <input type="submit" name="Submit" value="Submit" id="Submit">
</form>

I want the form to get submitted automatically when user select an option from the drop-down menu. How can I do this with or without using JavaScript (with or without jQuery)?

Thanks in advance... :)

blasteralfred

like image 840
Alfred Avatar asked Nov 30 '22 08:11

Alfred


2 Answers

Click (or select)? In that case the user would not be able to make any selection. You probably mean as soon as another option is selected. If so

<select name="dropdown" id="dropdown" onchange="this.form.submit()">

If jQuery is being used, unobtrusive event handler change should be used instead of inline javascript.

$(function(){
    $("#dropdown").change( function(e) {
        this.form.submit();
    });
});

Use on if the form elements are dynamically being added in the DOM

$(function(){
    $('#testform').on( "change", "#dropdown", function(e) {
        this.form.submit();
    });
});
like image 56
amit_g Avatar answered Dec 04 '22 08:12

amit_g


You will need to use the jQuery change() event.

('#dropdown').change( function() { ('#testform').submit(); })
like image 30
CtrlDot Avatar answered Dec 04 '22 09:12

CtrlDot