简体   繁体   English

如何使用JavaScript从表中获取单元格数据

[英]How to get data of a cell from a table using JavaScript

i am passing a very hard time with my web project.because i am new with web related languages. 我的网路专案经历了一段艰难的时期。因为我是网路相关语言的新手。 i just want to get data of a cell by clicking the same row button of the other cell. 我只想通过单击其他单元格的同一行按钮来获取该单元格的数据。 i am adding a pic please see this first. 我要添加图片,请先查看。

在此处输入图片说明

i try with many codes like below---(1st try) 我尝试使用下面的许多代码-(第一次尝试)

my js code- 我的js代码-

var tbl = document.getElementById("myTable");
        if (tbl != null) {

        for (var i = 0; i < tbl.rows.length; i++) {

                tbl.rows[i].cells[1].onclick = function (){ getval(this); };
        }
    }
    function getval(cell) {
    value(cell.innerHTML);
    }

my html code 我的html代码

<table class="w3-table-all w3-margin-top" id="myTable">
<tr>
  <th style="width:25%;">Vendor Picture Path</th>
  <th style="width:25%;">Vendor Heading</th>
  <th style="width:25%;">Vendor Body</th>
  <th style="width:25%;">Add courses</th>
</tr>

         echo '<tr>
         <td>'.$row["pic_path"].'</td>
         <td style="cursor: pointer;color:red;">'.$row["heading"].'</td>
         <td><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">'.$row["body"].'</div></td>
         <td><button>Add</button></td>                 
         </tr>';

my table data contains echo because i fatch the table data from my sql server. 我的表数据包含回显,因为我从SQL Server中添加了表数据。

my second try... js code 我第二次尝试... js代码

var tb2=document.getElementById("myTable");
        if(tb2 != null)
    {
        for(h=0;h<tb2.rows.length;h++)
        {
            bf=tb2.rows[h].cells[1];
            tb2.rows[h].cells[3].onclick=function(){getbtval(bf);};
        }
    }

             function getbtval(cell)
    {
       alert(cell.innerHTML);
    }     

and html code same... 1st one work for me.but that was not my expected result. 和html代码相同...第一个为我工作。但这不是我的预期结果。 my code success on second one result.but that fails.when i click every add button it gives me just the last value of 2nd cell last row and that is "ORACLE". 我的代码在第二个结果上成功。但是失败了。当我单击每个添加按钮时,它仅给出第二个单元格最后一行的最后一个值,即“ ORACLE”。 PLEASE TELL ME WHAT IS WRONG WITH MY CODE...... 请告诉我我的代码有什么错误......

Problem with your code is the fact you are not binding events to the button, you are picking a random cell of the table row. 代码的问题是您没有将事件绑定到按钮,而是选择了表行的随机单元格。 And the other issue is the fact you are not using var so it makes things global. 另一个问题是您没有使用var因此使事情变得全局化。

You said you want to click the button, but your code is not selecting the button. 您说要单击按钮,但是您的代码未选择按钮。 So instead of adding events all over the place, just use one and let event delegation take care of it. 因此,与其在整个地方添加事件,不如使用一个,然后让事件委托来处理它。 Check to see what triggered the event. 检查一下是什么触发了事件。 If it is a button, than select the row and than you can read the text of the cells. 如果是按钮,则选择行,然后您可以读取单元格的文本。

 document.getElementById("myTable").addEventListener("click", function(evt) { var btn = evt.target; if(btn.tagName==="BUTTON"){ var row = btn.parentNode.parentNode; //td than tr var cells = row.getElementsByTagName("td"); //cells console.log(cells[0].textContent, cells[1].textContent); } }); 
 <table class="w3-table-all w3-margin-top" id="myTable"> <tr> <th style="width:25%;">Vendor Picture Path</th> <th style="width:25%;">Vendor Heading</th> <th style="width:25%;">Vendor Body</th> <th style="width:25%;">Add courses</th> </tr> <tr> <td>123</td> <td style="cursor: pointer;color:red;">YYYY</td> <td> <div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">XXX</div> </td> <td> <button>Add</button> </td> </tr> <tr> <td>456</td> <td style="cursor: pointer;color:red;">dasdas</td> <td> <div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">qwwqeqwe</div> </td> <td> <button>Add</button> </td> </tr> </table> 

cell.onclick = function (){ getval(this); };

this means "current context", that is the cell which produced the click event. this表示“当前上下文”,即产生点击事件的cell

bf=tb2.rows[h].cells[1];
tb2.rows[h].cells[3].onclick=function(){getbtval(bf);};

After the for loop, bf points to the cell in the last row, so getval(bf) returns the value of it. 在for循环之后, bf指向最后一行中的单元格,因此getval(bf)返回它的值。

To access the proper cell, do the DOM traversal as @epascarello suggests. 要访问适当的单元格,请按照@epascarello的建议进行DOM遍历。 Depending on your use-case, it also might be easier to use data attribute: 根据您的用例,使用data属性也可能会更容易:

<button data-value="Cisco">

And then in the JS code 然后在JS代码中

button.onclick = function() { alert(this.dataset.value); }

You need to first identify the row clicked, for this you can check which element is current clicked by adding an event listener on the entire table and check when the target is button. 您需要首先确定单击的行,为此,您可以通过在整个表上添加事件侦听器来检查当前单击的元素,并检查目标何时为按钮。

Once you get the event target as button , find its parent td and its parent tr. 将事件目标作为button获取后,找到其父td和其父tr。

Now you got the tr and just loop through the child nodes, exclude the nodeType == 3 so that you only get the td element in the tr 现在,您获得了tr,并循环遍历了子节点,排除了nodeType == 3,以便仅在tr中获得td元素

 document.getElementById("myTable").addEventListener("click",function(e){ e = e || event var target = e.target || e.srcElement; if (target.nodeName != 'BUTTON') return var row = target.parentNode.parentNode; row.childNodes.forEach(function(item){ if(item.nodeType !== 3) { console.log(item.textContent); console.log(item.innerHTML); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="w3-table-all w3-margin-top" id="myTable"> <tr> <th style="width:25%;">Vendor Picture Path</th> <th style="width:25%;">Vendor Heading</th> <th style="width:25%;">Vendor Body</th> <th style="width:25%;">Add courses</th> </tr> <tr> <td>cisco-networking.jpg</td> <td style="cursor: pointer;color:red;">CISCO</td> <td><div style="width:100%;height: 60px;margin: 0;padding: 0;overflow-y: scroll">CISCO systems</div></td> <td><button>Add</button></td> </tr> </table> 

我通过为td指定一个id并使用jquery抓取文本来完成此操作

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

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