tags:

views:

21

answers:

2

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();

});
+1  A: 

You are submitting the select instead of the form:

$(".autosubmit select").change(function() {
   $(".autosubmit").submit();
});
Oded
+3  A: 

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();
});
VoteyDisciple
+1 for `closest` -- definitely the way to do this.
lonesomeday
it still does not submit. If I click submit, it works. But with auto-submit. What can it be the problem? I am generating output dynamically with PHP, but not Ajax. Do I need to add live()?
shin
You need to attach the `.change` handler while the form (and more specifically the targeted `select` box) are already on the page. You can either do that by defining the event handler **after** adding the form to the page, or, like you said, by using `.live()`
VoteyDisciple