jQuery如何获取td单元格值
jQuery How to get td cell value
当用户点击行的任何位置时,我正在尝试获取发票编号的值
下面是我的表
<div class="panel ui-widget-content" id="invoiceList">
<h2 class="ui-widget-header ui-corner-top" style="cursor: pointer; "><span>Invoices</span></h2>
<table cellspacing='0' id='header' class="ui-widget">
<tr>
<th>Invoice Number</th>
<th>Invoice Total</th>
</tr>
<tr>
<td><a href="#" >INV-Error_Test1</a></td>
<td>22.000000 USD</td>
</tr>
<tr>
<td><a href="#" >INV-Error_Test2</a></td>
<td>22.000000 USD</td>
</tr>
</table>
</div>
下面是我的jQuery,它只在点击Invoice Number
字段时才提供发票编号
$("#invoiceList td").click(function (e) {
var result = $(this).text();
console.log('invoice --->'+result);
});
http://jsfiddle.net/5n62md3m/
当用户点击行上的任何位置时,有人能帮我获取发票号码吗。
检查一下,这可能是一个解决方案:
$('tbody tr').on('click', function(e){
var value = $(this).find('td:first-child a').text();
alert(value);
e.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="panel ui-widget-content" id="invoiceList">
<h2 class="ui-widget-header ui-corner-top" style="cursor: pointer; "><span>Invoices</span></h2>
<table cellspacing='0' id='header' class="ui-widget">
<thead>
<tr>
<th>Invoice Number</th>
<th>Invoice Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#" >INV-Error_Test1</a></td>
<td>22.000000 USD</td>
</tr>
<tr>
<td><a href="#" >INV-Error_Test2</a></td>
<td>22.000000 USD</td>
</tr>
</tbody>
</table>
</div>
另一种替代解决方案可能是
$('#invoiceList tr:not(:first-child)').click(function(e){
$tds = $(this).find("td");
console.log( $tds.eq(0).text());
});
$("#invoiceList tr:not(:first-child)").click(function (e) {
console.log('invoice ---> '+$(this).children('td:nth-child(1) a').text());
}
这里的一个小好处是,如果你想获得另一列,你只能更改nth-child
中的数字(并可能删除后面的a
)。
相关文章:
- jQuery如何获取td单元格值
- 根据输入字段中键入的数字更改td单元格的颜色
- 在extjs网格4.0中获取textfield单元格td
- 如何将相应的表头 (th) 复制到他们的表单元格 (td)
- 在具有行标题和列标题的表中,给定列标题文本和单元格文本,如何选择单元格(td)
- 更改表格中单个单元格(TD)的CSS
- 根据表格单元格按钮动态访问 td
- 根据数据库中的值更改 td 单元格的颜色
- 如何在单元格 td HTML 中设置属性
- 获取上述td单元格的数据字段
- 为其中一个单元格或 td 查找 colSpan 标头是 Spans
- 如何通过JavaScript获取html表td单元格值
- 表单元格(td)相对于表行(tr)的绝对位置
- 选择单元格的各个部分<td>使用硒
- 单击按钮javaScript后更改td单元格的值
- 通过 Jquery 将文本附加到 td 单元格
- 使用jQuery从td单元格中获取文本
- 在javascript中,如何从td单元格中获取类名
- 使用Jquery从最后一个单元格中获取Td单元格
- 为什么它总是在AngularJS中选择第一个TD单元格