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();
});
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();
});
You are submitting the select
instead of the form:
$(".autosubmit select").change(function() {
$(".autosubmit").submit();
});
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.
精彩评论