我正在尝试使用this jQuery插件提交多选字段以呈现多个选项。
我的表格如下:
class BackupForm(forms.Form):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(BackupForm, self).__init__(*args, **kwargs)
self.fields['contas_choice'] = forms.MultipleChoiceField(
choices=get_backup_choices(self.user)
)
for field in self.fields:
self.fields[field].widget.attrs.update({'class': 'form-control', })
self.fields['contas_choice'].widget.attrs.update(
{
'name': 'from[]',
'id': 'search',
'class': 'form-control',
'size': '8',
'multiple': 'multiple',
}
)
contas_choice = forms.MultipleChoiceField(required=False)
# other fields
在我的模板上:
<form action="" class="form-horizontal" method="post">{% csrf_token %}
<div class="row">
<div class="col-xs-5">
{% for choice in form.contas_choice %}
{{ choice }}
{% endfor %}
</div>
<div class="col-xs-2">
<button type="button" id="search_rightAll" class="btn btn-block"><i
class="glyphicon glyphicon-forward"></i></button>
<button type="button" id="search_rightSelected" class="btn btn-block"><i
class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="search_leftSelected" class="btn btn-block"><i
class="glyphicon glyphicon-chevron-left"></i></button>
<button type="button" id="search_leftAll" class="btn btn-block"><i
class="glyphicon glyphicon-backward"></i></button>
</div>
<div class="col-xs-5">
<select name="to[]" id="search_to" class="form-control" size="8" multiple="multiple"></select>
</div>
</div>
<div id="actions" class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary">Baixar</button>
<a href="/listar-contas/" class="btn btn-default">Cancelar</a>
</div>
</div>
</form>
这会像我想要的那样呈现select字段,如插件引用的示例。
但是,当我尝试提交表单时,即使我将所有项目移至合适的方框。因此,我无法提交表单,我甚至不会收到任何Django错误,因为它无法立即验证HTML验证。
如何才能提交右侧框中的所有项目?
谢谢!