我不确定如何让 onchange 属性在下面的“position_type”选择小部件上正常工作。我希望它在更改字段选择时“发布”表单。目前使用此代码,浏览器在“onchange”触发时指示存在“页面错误”,我认为这意味着它无法识别结果“this.form.submit();” 命令。(我是按原样使用“this.form.submit();”来使用我发现的其他示例,还是应该以某种方式替换我的表单名称?)
class tranny_form(forms.Form):
def __init__(self, *args, **kwargs):
super(tranny_form, self).__init__(*args, **kwargs)
dict=args[0] #this extracts the settings passed to 'tranny_form' as a dict
position_list = [('GGA','GGA'),('DD','DD'),('UTM','UTM'),]
self.fields['position_type'] = forms.CharField(widget=forms.Select(choices=position_list, attrs={'onchange':'this.form.submit();'}))
if dict.get('position_type') in ['DD','UTM',]:
self.fields['easting_longitude_column'] = forms.IntegerField(max_value=100, min_value=1)
self.fields['northing_latitude_column'] = forms.IntegerField(max_value=100, min_value=1)
我的模板代码....
<form action="" enctype="multipart/form-data" method="post"> {% csrf_token %}
<table>
{{ form.as_table }}
</table> </br>
<input type="submit" value="Submit"></br>
</form>