[英]Getting a table row cell value when a specific column is is clicked
I was able to retrieve all row values via table click event and getting its value via event.currentTarget.cells[4].innerText();
我能够通过表click事件检索所有行值,并通过
event.currentTarget.cells[4].innerText();
获取其值event.currentTarget.cells[4].innerText();
. 。
But i would like to apply this if a specific column is clicked only like, when i clicked an ID 21
under Username column
. 但是,如果仅在单击“
Username column
下的ID 21
时才单击特定列,我想应用此功能。 It should alert all the cell values of the row. 它应该警告该行的所有单元格值。 And then when I clicked the other columns it should not alert.
然后,当我单击其他列时,它不应发出警报。
This is my code. 这是我的代码。 Please inform me if you are having problems the way I ask.
如果您遇到问题,请告知我。
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
alert(event.currentTarget.cells[0].innerText);
alert(event.currentTarget.cells[1].innerText);
alert(event.currentTarget.cells[2].innerText);
alert(event.currentTarget.cells[3].innerText);
alert(event.currentTarget.cells[4].innerText);
});
});
</script>
Here is my HTML http://jsfiddle.net/jE5UM/ 这是我的HTML http://jsfiddle.net/jE5UM/
I'd suggest, in the absence of specific HTML and other information: 我建议在没有特定的HTML和其他信息的情况下:
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
var cell = event.target,
values = $(cell).siblings().addBack().map(function(){
return $(this).text();
}).get();
alert(values);
});
});
References: 参考文献:
Try 尝试
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
$(this).children().each(function(){
alert($(this).text())
})
});
});
If you want an array as the result then 如果你想要一个数组作为结果
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
var texts = $(this).children().map(function(){
return $.trim($(this).html())
}).get();
});
});
Update 更新资料
$(document).ready(function () {
$('#tableid').on('click', 'tr td:nth-child(2)', function (event) {
var texts = $(this).closest('td').siblings().addBack().map(function(){
return $.trim($(this).html())
}).get();
alert(texts.join())
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.