romainberger
romainberger

Reputation: 4558

How to make a form to submit itself

I have a form with a <select> on the page. when the user selects an option, I want the form to submit itself. How can I do this? Thanks

Upvotes: 0

Views: 1125

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

You could subscribe for the .change() event on the dropdown and invoke the .submit() method of the containing form:

<form action="/" method="post">
    <select id="myDropdown">
        <option value="1">item 1</option>
        <option value="2">item 2</option>
        ...
    </select>

    ...
</form>

and then:

$(function() {
    $('#myDropdown').change(function() {
        $(this).closest('form').submit();
    });
});

Upvotes: 2

Umut
Umut

Reputation: 66

$('select').change(function(){$('form').submit();});

Needless to sayyou should be doing some more checking etc in there...

Upvotes: 0

Ryan Leonard
Ryan Leonard

Reputation: 997

<form name="foo">
    <select onchange="document.foo.submit();"></select>
</form>

Upvotes: 1

Related Questions