Django dropdownlist onchange submit

I am showing the user a dropdown 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 this is shown on the template as follows:

 <form method="post" onchange=change()> {{ days }} </form> 

I want to submit this form when the user selects an option from the drop-down list. For example, a user can be redirected to a new URL, and inside the program will write POST data. But all I find is related to the <select> , for example, when calling the javascript function when replacing the select element. But since the select element is in the form of ModelChoiceField, I do not know how to do this. Any help would be greatly appreciated!

+4
source share
2 answers

decided the following link

 days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('alias'), widget=forms.Select(attrs={"onChange":'refresh()'})) 
+9
source

Find the id dropdown using firebug . It should be id_days since you are using days name. Then bind the jQuery change event to it.

 $(function(){ $('#id_days').change(function(){ $('#id_of_form').submit(); }); }); 
+2
source

All Articles