繁体   English   中英

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

[英]Getting a table row cell value when a specific column is is clicked

我能够通过表click事件检索所有行值,并通过event.currentTarget.cells[4].innerText();获取其值event.currentTarget.cells[4].innerText();

但是,如果仅在单击“ Username column下的ID 21时才单击特定列,我想应用此功能。 它应该警告该行的所有单元格值。 然后,当我单击其他列时,它不应发出警报。

这是我的代码。 如果您遇到问题,请告知我。

<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>

这是我的HTML http://jsfiddle.net/jE5UM/

我建议在没有特定的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小提琴演示

参考文献:

尝试

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

演示: 小提琴

如果你想要一个数组作为结果

$(document).ready(function () {
    $('#tableid').on('click', 'tr', function (event) {                
        var texts = $(this).children().map(function(){
            return $.trim($(this).html())
        }).get();
    });
});

演示: 小提琴

更新资料

$(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM