jQuery: submit form after a value is selected from dropdown
Asked Answered
W

6

27

I have this form in my HTML:

<form action="/awe/ChangeTheme/Change" method="post">

    <select id="themes" name="themes">
        ...
        <option value="blitzer">blitzer</option>
    </select>

    <input type="submit" value="change" />

</form>

Anybody knows how to submit it when a value is selected in the 'themes' dropdown?

Wane answered 29/9, 2010 at 14:21 Comment(0)
S
78

The other solutions will submit all forms on the page, if there should be any. Better would be:

$(function() {
    $('#themes').change(function() {
        this.form.submit();
    });
});
Subtropics answered 29/9, 2010 at 14:28 Comment(2)
If you have multiple forms it would be even better if you replace the third line with: $(this).parent('form').submit();Nolte
@PeterdeRidder this.form already does that and more, because $(this).parent('form') only works if the form is the direct parent of the select, which it often isn't.Subtropics
A
6
$('#themes').change(function(){
    $('form').submit();
});
Adenaadenauer answered 29/9, 2010 at 14:24 Comment(0)
O
6

In case your html contains more than one form

$(function() {
  $('#themes').on('change', function(e) {
    $(this).closest('form')
           .trigger('submit')
  })
})
Oquendo answered 29/9, 2010 at 14:29 Comment(0)
K
2

I recommend using the longhand bind method because it has the same effect as the shorthand supplied by the other answers, but you can add additional events if need be without having to change your code.

$("#themes").bind("change", function() {
  $("form").trigger("submit");
});
Kajdan answered 29/9, 2010 at 14:26 Comment(0)
A
1
$(function() {
    $('#themes').change(function() {
        $('form').submit();
    });
});
Ammoniacal answered 29/9, 2010 at 14:24 Comment(0)
A
0
$(function() {
   $('#your_select_field_id').on('change', function(e) {
      $('#your_form_id').submit();
   })
})

This solved my problem efficiently.

Almonte answered 1/4, 2021 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.