【发布时间】:2021-09-13 08:34:34
【问题描述】:
我有一对多的相关数据(th 和 td),在页面加载时我使用 ajax 从服务器加载数据,我如何选择每个 th(列)并用链接的数据填充它。
这是桌子:
<table border="1" style="width: 95%;" id="table">
<tr id="ths">
{% for th in ths %}
<th>
<div data-th_id="{{ th.id }}">{{ th.name }}</div>
</th>
{% endfor %}
</tr>
<!-- populate with AJAX -->
<!-- <tr>
<td>
<div></div>
</td>
</tr> -->
<!-- END AJAX -->
</table>
AJAX:
$("table tr th").each(function () {
var th_id = $(this).children().data('th_id')
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "/api/load_tds",
data: JSON.stringify({
"th_id": th_id
}),
success: function (res) {
table = document.getElementById("table");
$.each(res, function (index, value) {
});
}
});
});
【问题讨论】:
-
使用 $(this).children().attr('data-th_id');获取值和 $(this).children().attr('data-th_id', 'valuetobeplaced');用于设置值
标签: javascript html jquery flask