如何基于我的Jinja for循环创建多个动态selectpicker?

时间:2019-05-22 01:14:46

标签: javascript html flask jinja2 bootstrap-selectpicker

当前,我正在尝试根据用户输入创建多个动态选择选择器下拉列表。此输入将通过jinja在for循环中传递到我的HTML页面。

基于for循环,我将创建相应数量的下拉列表对。

最重要的是,我还计划重新配置每个下拉列表的ID,以便可以轻松地在Flask代码中检索其值。我尝试用javascript编写for循环以修改ID。

我尝试编写一些javascript来实现这些功能,但似乎不起作用。实际上不是很熟悉javascript。

HTML代码

<form method="POST">
    {% for i in no_of_filters: %}
        <select id="col" class="selectpicker" onchange='refreshList(event,'{{ i+1|string }}')';>
            {% for c in cols: %}
                <option value="{{c}}">{{c}}</option>
            {% endfor %}
        </select>
        <select id ="val" class="selectpicker" multiple data-live-search="true">
            {% for uv in unique_val: %}
                <option value="{{uv}}">{{uv}}</option>
            {% endfor %}
        </select>
    {% endfor %}
    <input type="submit">
</form>
    <script>
        console.clear()

        var colElements = document.querySelectorAll('#col');
        var valElements = document.querySelectorAll('#val');

        for (var i = 0; i < colElements.length; i++)
            colElements[i].id = 'col' + (i+1);

        for (var j = 0; j < valElements.length; j++)   
            valElements[i].id = 'val' + (j+1);


        function refreshList(event, i){
            var col_select = document.getElementById('col'.concat(i));
            var uv_select = document.getElementById('val'.concat(i));

            column = col_select.value;

            fetch('/col/' + column).then(function(response) {
                response.json().then(function(data) {
                    var optionHTML = ' ';
                    for (var uv of data.unique_val) {
                        optionHTML += '<option value="' + uv.id + '">' + uv.value + '</option>';
                    }
                    uv_select.innerHTML = optionHTML;
                    $('#val'.concat(i)).on('change', function() {
                        $('#val'.concat(i)).selectpicker('refresh');
                    });
                })
            });
        }



    </script>

预期:如果no_of_filters = 7,则for循环应创建7对动态选择选择器下拉字段,并使用javascript配置ID

实际:仅创建1对选择选择器下拉列表,且未配置ID

1 个答案:

答案 0 :(得分:1)

选择具有相同的ID。尝试为每个选择添加不同的ID,如下所示:

HTML:

<form method="POST">
{% for i in no_of_filters: %}
    <select id="col-{{i}}" class="selectpicker col" 
    onchange='refreshList(event,'{{ i+1|string }}')';>
        {% for c in cols: %}
            <option value="{{c}}">{{c}}</option>
        {% endfor %}
    </select>
    <select id ="val-{{i}}" class="selectpicker val"
        multiple data-live-search="true">
        {% for uv in unique_val: %}
            <option value="{{uv}}">{{uv}}</option>
        {% endfor %}
    </select>
{% endfor %}
<input type="submit">
</form>

js:

<script>
        console.clear()

        var colElements = document.querySelectorAll('select.col');
        var valElements = document.querySelectorAll('select.val');

        for (var i = 0; i < colElements.length; i++)
            colElements[i].id = 'col-' + (i+1);

        for (var j = 0; j < valElements.length; j++)   
            valElements[i].id = 'val-' + (j+1);


        function refreshList(event, i){
            var col_select = document.getElementById('col-'.concat(i));
            var uv_select = document.getElementById('val-'.concat(i));
            // same logic
        }



    </script>