简体   繁体   English

单击特定列时获取表行单元格值

[英]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);
    });
});

JS Fiddle demo . JS小提琴演示

References: 参考文献:

Try 尝试

$(document).ready(function () {
    $('#tableid').on('click', 'tr', function (event) {                
        $(this).children().each(function(){
            alert($(this).text())
        })
    });
});

Demo: Fiddle 演示: 小提琴

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();
    });
});

Demo: Fiddle 演示: 小提琴

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())
    });
});

Demo: Fiddle 演示: 小提琴

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

相关问题 单击表行时如何从单元格获取值 - How to get the value from a cell when table row is clicked Javascript:单击时删除特定的表格单元格 - Javascript: Delete a specific table cell when it is clicked 单击时如何动态更改表单元格中的链接值时表格行的背景色 - how to dynamically change a table row background color when a link value within its cell changes when clicked JavaScript 单击同一行上的按钮时获取第二个单元格值 - JavaScript Getting second <td> cell value when clicked on a button on the same row 单击链接时增加特定行的值? - Increment a value of a specific row when a link is clicked? 如何在javascript中访问单击表行的特定单元格 - How to access specific cell of clicked table row in javascript jQuery,在表中单击超链接值时,将其从一个单元格复制到同一行中的另一个单元格 - jQuery, Copy hyper-link value when clicked on it in table from one cell to another in the same row 根据列单元格值清除特定行 - Clear specific row based on a column cell value html表,获取第一列相对于我单击的行的文本值 - html table, getting text value of first column relative to the row I clicked 获取单击列中第一个单元格的内容以及HTML表格中单击行的第一个单元格 - Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM