【发布时间】:2020-02-25 11:56:33
【问题描述】:
在test.html 中有一个id testTime 和jinja2 模板time(从test.py - render_template 返回)
<div>
<select class="test-default" id="testTime">
<option value=0>today</option>
<option value=7>week</option>
<option value=14>fortnight</option>
<option value=30>month</option>
</select>
</div>
{{ time }}
html中的脚本是,
function fetch_test_data(){
let test = $("#testTime").val();
axios({
method:"get",
url:"/test",
params: {
test
}
}).then(function (response) {
$(document.body).html(response.data)
});
}
向python文件发送值
test.py
@app.route('/test', methods=["GET"])
@login_required
def transactions_view():
time = (request.args.get("test"))
print(time) #this gives excepted output.
return render_template("test.html", time=time)
这会正确地从下拉列表中返回值。
我正在通过 jinja 模板 {{ time }} 将 time 渲染为 test.html。
但只要值在 html 中更新,dropdown 就会刷新而不显示所选值。
正确的值返回到烧瓶并渲染回来。但是下拉可视化/列表显示第一个值而不是选定的值?
如何修改以显示所选值而不是默认值/第一个值?
编辑:1
如果预先填充了<select> 下拉菜单怎么办?
这是我的html
<div>
<select class="test-default" id="testTime">
{% for option in options %}
<option value={{ option }}>{{ option }}</option>
{% endfor %}
</select>
</div>
还有test.py文件
@app.route('/test', methods=["GET"])
@login_required
def transactions_view():
options = ["0","7","14","30"]
return render_template("test.html", options=options)
在上述情况下如何使用selected属性。
【问题讨论】:
标签: javascript python html flask jinja2