I am showing to the user a dropdown list from this form:
class CronForm(forms.Form):
days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('day'))
class Day(models.Model):
day = models.CharField(max_length=200, default='')
month = models.CharField(max_length=200, default='')
def __unicode__(self):
return self.day
And It is shown at the template this way:
<form method="post" onchange=change()>
{{ days }}
</form>
What I want is to submit this form when the user selects an option from the dropdown list. For example, the user could be redirected to a new url, and internally the program would capture the POST data. But everything I find is related to the <select>
tag, like calling a javascript function onchange of the select element. But as the select element is IN the ModelChoiceField form, I don't know how to do it.
Any help would be very appreciated!
solved following this link
days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('alias'), widget=forms.Select(attrs={"onChange":'refresh()'}))
Find the id
of the dropdown using firebug. It should be id_days
as you are using the name days
. Then bind jQuery change
event to it.
$(function(){
$('#id_days').change(function(){
$('#id_of_form').submit();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With