首页 > 解决方案 > 当标题是{{变量}}时,如何使用Jquery从表中获取(标题)文本

问题描述

我看过一堆关于从标题中获取值/文本的帖子,但是没有一个解决方案适用于我的特定用例,我怀疑这可能是由于我的标题从我的底层获取了它的价值python代码(我正在使用烧瓶)。

<table id="UCNTable" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>
                    {{ header[0] }}
                    </th>
                    <th>
                    {{ header[2] }}
                    </th>
                    <th>
                    {{ header[3] }}
                    </th>
                    <th>
                    {{ header[4] }}
                    </th>
                    <th>
                    {{ header[5] }}
                    </th>
                </tr>
            </thead>
            <tbody>
                {% for row in data %}
                <tr>
                    <td>
                    {{row.name}}
                    </td>
                    <td>
                    {{row.Amt}}
                    </td>
                    <td>
                    {{row.OK}}
                    </td>
                    <td>
                    {{row.Err}}
                    </td>
                    <td>
                    {{row.prT}}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th>
                    {{ header[0] }}
                    </th>
                    <th>
                    {{ header[2] }}
                    </th>
                    <th>
                    {{ header[3] }}
                    </th>
                    <th>
                    {{ header[4] }}
                    </th>
                    <th>
                    {{ header[5] }}
                    </th>
                </tr>
            </tfoot>
        </table>

   <script>
        $('#UCNTable tfoot th').each( function () {
            var title = $(this).text();
            $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        } );
</script>

所以基本上,这应该说“搜索变量”,但它只是说搜索。关于我做错了什么的任何想法?我怀疑 JQuery 正在寻找一些文本,但它有一个问题,因为它是一个变量 - 任何帮助表示赞赏!谢谢。

标签: jqueryhtmlflaskdatatables

解决方案


我找到了解决办法!事实证明,在我的函数中,当变量被添加到占位符时,不知何故出现了一个流氓 /n 换行符,所以我修剪了变量以让它正确显示结果

$('#UCNTable tfoot th').each( function () {
        var title = $(this).text();         
        var trimTitle = $.trim(title.replace(/[\t\n]+/g,' '));

        $(this).html( '<input type="text" placeholder="Search '+trimTitle+'"/>' );
    } );

推荐阅读