[英]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.