简体   繁体   English

单击表格行并获取所有单元格的值

[英]Click table row and get value of all cells

I don't know JQuery, so I'm hoping there is a way to do this in pure Javascript.我不知道 JQuery,所以我希望有一种方法可以在纯 Javascript 中做到这一点。

I need to click on a table row and get the value of each cell in that row.我需要单击表格行并获取该行中每个单元格的值。 Here is the format of my table:这是我的表的格式:

<table class='list'>
    <tr>
        <th class='tech'>OCB</th>
        <th class='area'>Area</th>
        <th class='name'>Name</th>
        <th class='cell'>Cell #</th>
        <th class='nick'>Nickname</th>
    </tr>
    <tr onclick="somefunction()">
        <td>275</td>
        <td>Layton Installation</td>
        <td>Benjamin Lloyd</td>
        <td>(801) 123-456</td>
        <td>Ben</td>
    </tr>
</table>

Is there anyway short of putting a unique ID to each cell?无论如何都没有为每个单元格放置一个唯一的 ID 吗?

There is no need to add ids or add multiple event handlers to the table.无需向表中添加 id 或添加多个事件处理程序。 One click event is all that is needed.一键事件就是所需要的。 Also you should use thead and tbody for your tables to separate the heading from the content.此外,您应该对表格使用 thead 和 tbody 以将标题与内容分开。

 var table = document.getElementsByTagName("table")[0]; var tbody = table.getElementsByTagName("tbody")[0]; tbody.onclick = function (e) { e = e || window.event; var data = []; var target = e.srcElement || e.target; while (target && target.nodeName !== "TR") { target = target.parentNode; } if (target) { var cells = target.getElementsByTagName("td"); for (var i = 0; i < cells.length; i++) { data.push(cells[i].innerHTML); } } alert(data); };
 <table class='list'> <thead> <tr> <th class='tech'>OCB</th> <th class='area'>Area</th> <th class='name'>Name</th> <th class='cell'>Cell #</th> <th class='nick'>Nickname</th> </tr> </thead> <tbody> <tr> <td>275</td> <td>Layton Installation</td> <td>Benjamin Lloyd</td> <td>(801) 123-456</td> <td>Ben</td> </tr> </tbody> </table>

Example:例子:

http://jsfiddle.net/ZpCWD/ http://jsfiddle.net/ZpCWD/

Check this fiddle link检查这个小提琴链接

HTML: HTML:

<table id="rowCtr" class='list'>
    <thead>
        <tr>
            <th class='tech'>OCB</th>
            <th class='area'>Area</th>
            <th class='name'>Name</th>
            <th class='cell'>Cell #</th>
            <th class='nick'>Nickname</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>275</td>
            <td>Layton Installation</td>
            <td>Benjamin Lloyd</td>
            <td>(801) 123-456</td>
            <td>Ben</td>
        </tr>
    </tbody>
</table>

JAVASCRIPT:爪哇脚本:

init();
function init(){

    addRowHandlers('rowCtr');

}

function addRowHandlers(tableId) {
    if(document.getElementById(tableId)!=null){
        var table = document.getElementById(tableId);
        var rows = table.getElementsByTagName('tr');
        var ocb = '';
        var area = '';
        var name = '';
        var cell = '';
        var nick = '';
        for ( var i = 1; i < rows.length; i++) {

            rows[i].i = i;
            rows[i].onclick = function() {

                ocb = table.rows[this.i].cells[0].innerHTML;                
                area = table.rows[this.i].cells[1].innerHTML;
                name = table.rows[this.i].cells[2].innerHTML;
                cell = table.rows[this.i].cells[3].innerHTML;
                nick = table.rows[this.i].cells[4].innerHTML;
                alert('ocb: '+ocb+' area: '+area+' name: '+name+' cell: '+cell+' nick: '+nick);
            };
        }
    }
}
$("tr").click(function () {
    var rowItems = $(this).children('td').map(function () {
        return this.innerHTML;
    }).toArray();
});

This shows the row's first cell which is clicked according to dataTr.querySelectorAll("td")[0].innerText;这显示了根据 dataTr.querySelectorAll("td")[0].innerText; 单击的行的第一个单元格;

document.querySelector("#myTable").addEventListener("click",event => {
    let dataTr = event.target.parentNode;
    let dataRes = dataTr.querySelectorAll("td")[0].innerText;
    console.log(dataRes);
});
var elements  = document.getElementsByTagName('td');
for (var i =0; i < elements.length; i++) {
   var cell_id = 'id' + i;
   elements[i].setAttribute('id', cell_id);
}

Maybe put something like this in function your onclick links to from the tr ?也许把这样的东西放在你的 onclick 链接到tr函数中?

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

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