[英]jQuery - Get the data from a cell in a selected row
I have this snippet of code, testing whether or not I am getting results: 我有以下代码段,用于测试是否获得结果:
$("#texts tr").on("click", function(evt){
var parent_id = $(("td")[0]).text();
alert(parent_id);
});
And it delivers nothing, so I imagine I have constructed my attempt at retrieving the data improperly? 它什么也没有提供,所以我想我已经构造了尝试不正确地检索数据的尝试吗?
UPDATE: html (generated using jade views in Node) 更新:html(使用Node中的jade视图生成)
extends layout
//- Format for displaying list of texts
block content
h1 Equity Texts
table.table.table-striped.table-hover.table-bordered
thead
tr
th Objid
th Book Title
tbody#texts
each text in texts
tr(id = "book_" + text.objid)
td= text.objid
td= text.book_title
script(src="/jquery.min.js",type="text/javascript")
script(src="/handler.js",type="text/javascript")
As far as I understand, you want to get text of the first td of the clicked tr. 据我了解,您想获取单击的tr的第一个td的文本。 Try this instead:
尝试以下方法:
$("#texts tr").on("click", function(){
var parent_id = $(this).find('td:first').text();
alert(parent_id);
});
You can replace td:first
with td:eq(0)
您可以将
td:first
替换为td:eq(0)
The selector is t
which doesn't select any elements. 选择器是
t
,它不选择任何元素。 The code snippet ("td")[0]
returns t
and you are passing it as a selector to jQuery. 代码段
("td")[0]
返回t
,您将其作为选择器传递给jQuery。
For getting text content of the first td
element you can code: 为了获得第一个
td
元素的文本内容,您可以编写以下代码:
// you probably wanted to use this syntax
$( $('td')[0] ).text();
// other alternatives
$("td").eq(0).text();
$("td:eq(0)").text();
$("td").first().text();
Just found another answer that works also. 刚刚找到了另一个可行的答案。 Thanks for the assist.
感谢您的协助。
http://stackoverflow.com/questions/15615825/how-to-access-specific-cell-of-clicked-table-row-in-javascript http://stackoverflow.com/questions/15615825/how-to-access-specific-cell-of-clicked-table-row-in-javascript
I didn't know about the cells property. 我不知道细胞的性质。 If that isn't deprecated at this point.
如果现在不建议这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.