简体   繁体   English

如何使用 jQuery 从 HTML 表格的倒数第二行获取单元格值

[英]How to get the cell value from an HTML table's second last row using jQuery

I am trying to get the value of the first cell in the second last row of my table.我正在尝试获取表格倒数第二行第一个单元格的值。 The rows are created dynamically (in a Django project) and I am using the each addition of a row as the trigger to get the cell value for further processing.行是动态创建的(在 Django 项目中),我使用每行添加作为触发器来获取单元格值以进行进一步处理。

To get the last row, first cell value, I could use:要获取最后一行,第一个单元格值,我可以使用:

var first_bull = $('#my_table tr:last').find('td:first').text();

I can construct a hash on the fly and get the value but what I am trying to do is using a simpler, one line code that could do the job, if that is possible.我可以即时构建一个散列并获取值,但我想要做的是使用一个更简单的单行代码,如果可能的话,它可以完成这项工作。

How do I get the first cell's value of the second last row?如何获取倒数第二行的第一个单元格的值? I used .prev() but drawing blank though there is value available in the cell.我使用.prev()但绘制空白,尽管单元格中有可用值。

Also in the code above, .val() doesn't work, whereas the following works:同样在上面的代码中, .val()不起作用,而以下代码有效:

varText = $('#id_my_txt').val();

Why?为什么?

I think $('#id_my_txt').val() works because $('#id_my_txt') is input or textarea element.我认为$('#id_my_txt').val()起作用是因为$('#id_my_txt')是输入或 textarea 元素。

To get value of input present in second last row and first column, you should try:要获取出现在倒数第二行和第一列中的输入值,您应该尝试:

$('#my_table tr:last').prev().find('td').eq(0).find('input').val()
// To get cell text present inside second last cell:
$('#my_table tr:last').prev().find('td').eq(0).text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jQuery获取HTML表格的第一行的最后一列 - How to get the first row's last column of an Html table using jQuery 如何使用javascript连续获取HTML表格单元格上的Dropdownlist值 - How to get Dropdownlist value on HTML Table Cell in a Row using javascript 如何使用jQuery从html表行的第5个单元格中获取数据 - How to get data from the 5th cell in a html table row using jQuery 使用jQuery,如何引用html表行的倒数第二列或按类名? - Using jQuery, how could I reference the second to last column of a html table row or by class name? 如何使用jQuery从表行中的特定单元格中获取复选框值? - How do I get the checkbox value from a specific cell in a table row using jQuery? 如何使用绝对行号从复选框中使用jQuery获取表单元格的值 - How to get value of table cell with jquery from checkbox using absolute row number jquery:在表的倒数第二行中使用 appendTo - jquery: using appendTo in second to last row of table 如何使用jQuery获取html表的最后一个可见子表行? - How to get the last visible child table row of a html table with jQuery? 获取表格最后一行中单元格的值 - get value of the cell in the last table row 如何在行单击时使用 jquery 获取表格单元格值并将值显示到另一个表格? - How to get table cell value using jquery on row click and show the value to another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM