烧瓶视图:
Method m = .. getMethod("getValues");
Object o = m.invoke(modelObject);
ArrayList<?> classValues = (ArrayList<?>) o
hello.html - 我正在使用Flask-Bootstrap
@app.route("/")
def hello():
return render_template('hello.html',
form=form,
working_days=['Monday', 'Tuesday', 'Wednesday', 'Friday'])
会产生:
<form class="form-group" action="..." method="post">
{{ form.csrf_token }}
{{ wtf.form_field(form.days) }} <!-- saturday, sunday, monday, tuesday, wensday, thursday, friday -->
</form>
我的问题是如何将默认值(<select class="form-control" id="days" multiple name="days" required>
<option value="saturday">saturday</option>
<option value="sunday">sunday</option>
<option value="monday">monday</option>
<option value="tuesday">tuesday</option>
<option value="wednesday">wednesday</option>
<option value="thursday">thursday</option>
<option value="friday">friday</option>
</select>
)设置为working_days
?
答案 0 :(得分:0)
这里是答案(纯javascript),所以我需要做的就是循环选项值并检查是否与working_days
中的值匹配,如果是,我将添加attr('selected', true)
< / p>
其中$('.form-group')
是加载页面时已存在的最近父级。
for (var i = 1; i < $('.form-group').find('option').length + 1; i++){
if ({{ working_days | safe }}.indexOf($('.form-group').find('option:nth-child(' + i + ')').val()) > -1) {
$('.form-group').find('option:nth-child(' + i + ')').attr("selected", true)
}
}
:)