代码之家  ›  专栏  ›  技术社区  ›  BruceWayne

HTML选择-稍后在HTML中使用当前选择值,带烧瓶

  •  0
  • BruceWayne  · 技术社区  · 6 年前

    index.html索引

    <div id="search_terms">
        <select onchange="refresh_selection()" id="query_selection">
            {% for term in search_terms %}
            <option value="{{term}}">{{term}}</option>
            {% endfor %}
        </select>
        <select id="news_date_selection">
        {% if [THE OPTION SELECTED ABOVE] in html_files %}
            <option value="{{file}}">{{file}}</option>
        {% endif %}
        </select>
    </div>
    

    其思想是在我的Flask.py文件中使用一个函数来生成一个文件名列表( html_files

    取决于 query_selection html文件 会有所不同。

    所以,如果 查询选择 选择是“哥谭”,我希望这个条件语句是

    {% if "Gotham" in html_files %}
    

    $("#query_selection).on("change", function(){}) 或者类似的事情?)

    1 回复  |  直到 6 年前
        1
  •  1
  •   shifloni    6 年前

    你应该使用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>