jquery - 当标题是{{变量}}时,如何使用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 正在寻找一些文本,但它有一个问题,因为它是一个变量 - 任何帮助表示赞赏!谢谢。
解决方案
我找到了解决办法!事实证明,在我的函数中,当变量被添加到占位符时,不知何故出现了一个流氓 /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+'"/>' );
} );
推荐阅读
- typescript - TypeScript 函数仅接受具有由其他参数指定的属性且该属性是数字的对象数组
- ruby-on-rails - 如何使用 Capybara attach_file 将文件附加到 Rails 中的嵌套表单
- c# - ExecuteNonQuery() 没有正确更新
- java - 你可以在没有活页夹(Kafka/RabbitMQ)的情况下使用 Spring-Cloud-Stream 3 吗?
- azure-data-factory - Azure REST API - 仅返回 500 行
- flutter - 为什么 setState 不重建我的子小部件?
- alert - 我希望指标在 HullMA 改变颜色时发送买入/卖出信号
- javascript - 如何使用 JavaScript 通过子字符串选择变量
- url - 通过 3rd 方应用程序共享时,LinkedIn 无法识别 OG 标签
- python - 使用基于列的条件查找 Pandas 中各行之间的日期范围重叠