jquery - 获取在 jquery.click() 事件中单击的行的详细信息
问题描述
我有一个动态表,具有可变的行数。我正在尝试根据单击的特定行从后端获取相关信息。
表的 html(这是来自 django 模板 - 基本上在返回的 db 中为每个对象添加一个):
<table class="table table-bordered" id="dataTable">
<thead>
<tr>
<th>pk</th>
<th>ent_1</th>
<th>ent_2</th>
<th>ent_3</th>
</tr>
</thead>
<tbody>
{% for o in object_list %}
<tr>
<td><a class="test" href="{% url 'myapp:ajaxview' %}">{{ o.pk }}</a></td>
<td><a class="test" href="{% url 'myapp:ajaxview' %}">{{ o.ent_1 }}</a></td>
<td><a class="test" href="{% url 'myapp:ajaxview' %}">{{ o.ent_2 }}</a></td>
<td><a class="test" href="{% url 'myapp:ajaxview' %}">{{ o.ent_3 }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div id="details">
<!-- details info from the back is appended to the DOM here. -->
</div>
捕获事件并将 html 附加到 div#details 的 jquery 代码:
$(function() {
$('.test').click(function (event) {
event.preventDefault();
console.log("table row clicked") // sanity check
update_details();
});
function update_details() {
console.log("update details is called")
$.ajax({
// must include the app endpoint at the beginning, others it appends to current url, e.. ajaxlist/get_ajax_details/
url: "/myapp/get_ajax_details/",
type: "GET",
data: {},
// success
success: function (rendered_template) {
$('#details').empty();
$('#details').prepend(rendered_template)
console.log(rendered_template);
console.log("success")
},
// error
error: function (xhr, errmsg, err) {
$('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: " + errmsg +
" <a href='#' class='close'>×</a></div>"); // add the error to the dom
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
};
});
我已经将它踩到了萤火虫中,但是我不明白如何才能获得单击了哪一行的完整详细信息。最终,我需要单击的那一行的第一列(“pk”列)的内容,以便我可以将其发送到支持的,以便它返回带有相关信息的 render_template,我可以将它们附加到我的div#详细信息。
其他详情:
我正在为选择器使用类。我知道我可以改为生成一个整数 ID(例如,为每行编号)。但这似乎很浪费,我确信我的目的不需要它。这是一个快速原型,最终我将研究 jquery 选择器,这样我只需要向表本身添加一个类(然后在需要监听的表中监听任何 table>tbody>tr>a 的点击)。
谢谢
解决方案
稍微修改您的代码并将点击事件附加到“td”而不是“a”标签以轻松获取它的索引。
$(document).ready(function(){
$('#dataTable td').click(function (event) {
var col = $(this).index(),
firstColVal = $(this).parent().children('td:first-child').text(),
row = $(this).parent().index();
console.log("Row index: " + row + ", Col index: " + col + ", Col Value: " + firstColVal);
//update_details(); <-- pass required value as parameter
});
});
table {
border-collapse: collapse;
}
th, td { border: 1px solid #000; padding: 10px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered" id="dataTable">
<thead>
<tr>
<th>pk</th>
<th>ent_1</th>
<th>ent_2</th>
<th>ent_3</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="test" href="#">PK Row 0</a></td>
<td><a class="test" href="#">Ent_1</a></td>
<td><a class="test" href="#">Ent_2</a></td>
<td><a class="test" href="#">Ent_3</a></td>
</tr>
<tr>
<td><a class="test" href="#">PK Row 1</a></td>
<td><a class="test" href="#">Ent_1</a></td>
<td><a class="test" href="#">Ent_2</a></td>
<td><a class="test" href="#">Ent_3</a></td>
</tr>
<tr>
<td><a class="test" href="#">PK Row 2</a></td>
<td><a class="test" href="#">Ent_1</a></td>
<td><a class="test" href="#">Ent_2</a></td>
<td><a class="test" href="#">Ent_3</a></td>
</tr>
</tbody>
</table>
推荐阅读
- bootstrap-carousel - 你可以让 Bootstrap Carousel 通过刷新而不是按时间旋转来更改广告吗?
- go - 如何使用 Pipes 分配额外的 ExraFile 以拥有多个输出流?
- java - 在 vaadin 聊天对话框中输出 hello (Spring boot 和 spring scheduler + vaadin 14)
- python - 如何将 JPG 图像的整数数组转换为 JPG 图像?
- ballerina - Ballerina 数组操作(如 push 和 pop)是线程安全的吗?
- arm - 保存 Cortex-M4 处理器状态以进行断电和恢复
- javascript - 使用 js SDK 向 Amazon Lex 发送额外的详细信息
- excel - 如何使用 Excel VBA 设置一个变量以过滤不同的文本字符串值?
- tensorflow - 如何在张量流中生成 VGG16 多特征
- wordpress - Wordpress:自动在帖子中添加来自 youtube 视频的图像