shin
shin

Reputation: 32721

Auto-submitting a form with jquery

I have the following form. And the form works. Now I want to auto-submit the form when I select one of option.

<div id='weeksubmit'><!-- I don't need this div -->
<form action="http://localhost/myapplication/index.php/courses/admin/index" method="post" class="autosubmit">
<label for='weekid'>Select Week</label>
<select name="weekid">
<option value="1">Uke 40 (04/10 - 10/10)</option>
<option value="2" selected="selected">Uke 41 (11/10 - 17/10)</option>
<option value="3">Uke 42 (18/10 - 24/10)</option>
<option value="4">Uke 43 (25/10 - 31/10)</option>
</select>
<input type="submit" name="submit" value="Change Week"  /></form>
</div>

I tried the following code, but it does not work.

Could anyone tell me what I am doing wrong?

Thanks in advance.

$(".autosubmit select").change(function() {
   $(this).submit();

});

Upvotes: 2

Views: 8779

Answers (3)

Brendon Muir
Brendon Muir

Reputation: 4612

A slight adaptation of Votey's answer to use data- attributes and live watching:

$("form[data-autosubmit] select").live('change', function() {
    $(this).closest('form').submit();
});

You should give him the correct answer though.

Upvotes: 1

VoteyDisciple
VoteyDisciple

Reputation: 37803

Your .change() function is correctly placed on the select element, but that means that $(this) is the same select element. You can't submit a drop-down box. How about:

$(".autosubmit select").change(function() {
    $(this).closest('form').submit();
});

Upvotes: 7

Oded
Oded

Reputation: 499002

You are submitting the select instead of the form:

$(".autosubmit select").change(function() {
   $(".autosubmit").submit();
});

Upvotes: 4

Related Questions