简体   繁体   English

Django表单中的可编辑选择字段/下拉框

[英]Editable choice field/drop down box in Django forms

I am very new to Django. 我是Django的新手。 I want to create a editable drop down box. 我想创建一个可编辑的下拉框。 After searching I found the below code to create the dropdown list. 搜索后,我找到了以下代码来创建下拉列表。

Choices1 = [("0", _("0")), ("1", _("1")),("2", _("2"))[("3", _("3"))]

_list =  forms.ChoiceField( choices=choices1,label=_("ListExample"),required=False)

It displays the drop down box. 它会显示下拉框。 But this is not an editable field. 但这不是一个可编辑的领域。 It does not allow me to edit the value , it just allows me to select. 它不允许我编辑值,它只允许我选择。 I want to make this dropdown/choice filed as editable box. 我想将此下拉列表/选项归档为可编辑框。

Thanks, Kalai 谢谢,Kalai

maybe you can do: 也许你可以这样做:

Choices1 = [("0", _("0")), ("1", _("1")),("2", _("2"))[("3", _("3"))]

options = (value for key, value in Choices1 )

list =  forms.CharField(widget=forms.Textarea(attrs{'selectBoxOptions':';'.join(options)})),label=_("ListExample"),required=False)

in your html, you could try: 在您的HTML中,您可以尝试:

<form>
    {{form.as_p}}
</form>


<script type="text/javascript">
    createEditableSelect(document.forms[0].myText);
</script>

For future reference: the solution is now available with html5 which allows editable dropdown list. 供将来参考:该解决方案现在提供html5,允许可编辑的下拉列表。 And so with Django: https://stackoverflow.com/a/32791625/1937033 对于Django: https//stackoverflow.com/a/32791625/1937033

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM