【问题标题】:How to get th (header) text from a table using Jquery, when header is a {{ variable }}当标题是{{变量}}时,如何使用Jquery从表中获取(标题)文本
【发布时间】:2019-02-10 11:23:19
【问题描述】:

我看过一堆关于从标题中获取值/文本的帖子,但没有一个解决方案适用于我的特定用例,我怀疑这可能是由于我的标题获得了它的值来自我的底层 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 正在寻找一些文本,但它有一个问题,因为它是一个变量 - 任何帮助表示赞赏! 谢谢。

【问题讨论】:

  • 你能告诉我们你烧瓶代码的输出 html吗?
  • @Mosh Feu 呃,是的,当然!图片可以吗? i.imgur.com/FJ9wUJO.png 如果我从 title 变量中删除 .text() 它会说“搜索 [object Object]
  • 输出 html :) 所以我们可以尝试调试它。
  • @Mosh Feu 抱歉,我不是网络开发人员,所以我不确定这到底意味着什么!通过在我的页面上检查 (CTRL+SHIFT+I) 可以得到输出 html 吗?
  • @Mosh Feu(我无法编辑我的评论,所以我再写一次)似乎在搜索和 variable &lt;input type="text" placeholder="Search ERRORProducts "&gt; 之间出现了一个流氓换行符

标签: jquery html flask datatables


【解决方案1】:

我找到了解决办法! 事实证明,在我的函数中,当 variable 被添加到占位符时,不知何故出现了一个流氓 /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+'"/>' );
    } );

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多