what I want to do, this I have a drop down box that lets my admin users rate pictures G rated and X rated. once they have selected this option I would like the form to be submitted.
I thought it would be as easy as sending the form ID, but seems not.
this is the only thing 开发者_如何转开发the user needs to do in the form.
the html form looks like this
<form id="photo1">
<input id="1" name="pictureid" hidden />
<select name="phototype">
<option value="G">G Rated</option>
<option value="X">X Rated</option>
</select>
</form>
If you want to submit the form when the onchange event is fired, this is the code:
$('#photo1 select').change(function () {
$("#photo1").submit();
});
$('#photo1 select').change(function() {
$('#photo1').submit();
return true;
});
This should be enough
$('select[name="phototype"]').change(function(){
this.form.submit();
});
It finds the relative form automatically, so it can apply to multiple forms as well.
Or if you want this with .live()
due to loading forms dynamically, then use
$('select[name="phototype"]').live('change',function(){
this.form.submit();
});
The problem with your code currently is that your user can't select "G-rated", because it's selected by default. You should add a blank option. You probably also need a value for your hidden element:
<form id="photo1">
<input id="1" name="pictureid" value="1" hidden />
<select name="phototype" id="phototype">
<option value=""></option>
<option value="G">G Rated</option>
<option value="X">X Rated</option>
</select>
</form>
You could then use the following jQuery:
$('#phototype').change(function() {
if (this.value) {
$(this).closest('form').submit();
}
});
jsFiddle
working demo
$('select').change(function(){
$(this).closest('form').submit();
});
$('select').live('change',function(){
$(this).closest('form').submit();
});
精彩评论