Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django dropdownlist onchange submission

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!

like image 999
toni Avatar asked Apr 18 '13 06:04

toni


2 Answers

solved following this link

days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('alias'), widget=forms.Select(attrs={"onChange":'refresh()'}))
like image 129
toni Avatar answered Oct 19 '22 23:10

toni


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();
    });
});
like image 25
arulmr Avatar answered Oct 20 '22 01:10

arulmr