【发布时间】:2018-05-25 01:20:51
【问题描述】:
我刚开始使用 Flask WTF 表单。我可以和他们一起做我需要的一切,除了我似乎无法弄清楚一件事。我有一个多项选择字段,向用户呈现各种选项,如果用户选择“其他”,我希望他们描述他们的意思。像这样:
impact = wtforms.SelectMultipleField('What is the expected impact?',
choices=[('Sales', 'Sales'),
('Lift', 'Lift'),
('Other', 'Other')]
我不知道当它不是具有自己 ID 的独立字段而只是数组中的成员时,它是否可能。你能帮忙吗?
编辑
这是我按照以下建议尝试的方法 - 在选择或取消选择“其他”没有任何区别的意义上它不起作用:
app.py:
app.route('/', methods=['GET', 'POST'])
def home():
form = MyForm()
other_enable = False
if form.validate_on_submit():
name = form.name.data
email = form.email.data
impact1 = form.impact.data
impact = ''.join(str(e) for e in impact1)
if ("Other" in impact):
other_enable = True
impact_other = form.impact_other.data
return redirect(url_for('home'))
return(render_template('home.html', form=form, other_enable=other_enable))
并且templates/home.html包含
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
<center>
<div id="someid" onchange='myFunction(other_enable)'>{{ wtf.quick_form(form) }}</div>
</center>
{% endblock %}
<script>
function myFunction(other_enable) {
var theother = document.getElementById("otherField");
if (other_enable == true){
theother.style.display = "block";
} else {
theother.style.display = "none";
}
}
</script>
【问题讨论】:
标签: flask flask-wtforms