你应该使用Ajax。你的方向是对的。使用
$("#query_selection).on("change", function(){})
使用第一个选择框的选定值向将返回第二个选择框选项的端点进行ajax调用
参见代码示例:
您的JS文件:
$('#select1').change(function () {
$.ajax({
url: url, // this is the variable we declared in the template file
data: $('#select1 option:selected').val(),
type: 'POST',
contentType: false
}).done(function (res) {
// assuming res is the options for the second select filed
// append them to the second select
});
});
from flask import json
@app.route('/get_options', methods=["GET", "POST"])
def get_options():
select1_value = request.data
# make a query with the selected value
return json.dumps(query_results)
将此tp添加到HTML模板
<script>
var url = "{{url_for('get_options')}}";
</script>