javascript django dropdownlist onchange 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16076420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:11:50  来源:igfitidea点击:

django dropdownlist onchange submission

javascriptpythonhtmldjangohtml-select

提问by toni

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!

我想要的是当用户从下拉列表中选择一个选项时提交此表单。例如,用户可以被重定向到一个新的 url,程序将在内部捕获 POST 数据。但我发现的一切都与<select>标签有关,比如调用 select 元素的 javascript 函数 onchange。但是由于 select 元素在 ModelChoiceField 表单中,我不知道该怎么做。任何帮助将不胜感激!

回答by toni

solved following this link

解决了这个链接

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

回答by arulmr

Find the idof the dropdown using firebug. It should be id_daysas you are using the name days. Then bind jQuery changeevent to it.

id使用firebug查找下拉列表的。它应该id_days与您使用的名称相同days。然后将 jQuerychange事件绑定到它。

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