简体   繁体   English

如何从 html 表中获取值?

[英]How to get value from html table <tr>?

I have a website that need to validate the table header name.我有一个网站需要验证表 header 名称。 For example, it wont allow user to click a button if the HTML consist a header name Group1 .例如,如果 HTML 包含 header 名称Group1 ,则不允许用户单击按钮。 So my question is how can I find the table header name Group1 in the <tr> using Javascript?所以我的问题是如何使用 Javascript 在<tr>中找到表 header name Group1

Full HTML:完整 HTML:

<table class="table table-striped table-bordered" id="tableid">
   <tbody>
      <tr>
         <th>Device</th>
         <th>Serial</th>
         <th>ID</th>
         <th>Name</th>
         <th>Groups</th>
         <th>Device</th>
         <td>Group1</td>
         <td>Group2</td>
      </tr>
      <tr>
         <td>TY83-FPSX-C3WS</td>
         <td>xxx1</td>
         <td>test1</td>
         <td>John</td>
         <td> Driver</td>
         <td>DKFU-V7ZE-RD9R</td>
         <td>
            <div class="col" classname="col">
               <select name="group1" id="groupId">
                  <option>Null</option>
                  <option value="IT">IT</option>
                  <option value="Cleaning">Cleaning</option>
                  <option value="Accountant">Accountant</option>
               </select>
            </div>
         </td>
         <td>
            <div class="col" classname="col">
               <select name="group1" id="groupId">
                  <option>Null</option>
                  <option value="IT">IT</option>
                  <option value="Cleaning">Cleaning</option>
                  <option value="Accountant">Accountant</option>
               </select>
            </div>
         </td>
      </tr>
      <tr>
         <td>4SB9-NR2D-742E</td>
         <td>xxx2</td>
         <td>test2</td>
         <td>Cena</td>
         <td>Telesales</td>
         <td>DqwdKFU-VqwdZE-RD9R</td>
         <td>
            <div class="col" classname="col">
               <select name="group1" id="groupId">
                  <option>Null</option>
                  <option value="IT">IT</option>
                  <option value="Cleaning">Cleaning</option>
                  <option value="Accountant">Accountant</option>
               </select>
            </div>
         </td>
         <td>
            <div class="col" classname="col">
               <select name="group1" id="groupId">
                  <option>Null</option>
                  <option value="IT">IT</option>
                  <option value="Cleaning">Cleaning</option>
                  <option value="Accountant">Accountant</option>
               </select>
            </div>
         </td>
      </tr>
   </tbody>
</table>

If there is no specific reason for using td in Group1 and Group2 then this can be modified to:如果没有在 Group1 和 Group2 中使用 td 的特定原因,则可以将其修改为:

Original HTML :原始 HTML :

 <tr>
     <th>Device</th>
     <th>Serial</th>
     <th>ID</th>
     <th>Name</th>
     <th>Groups</th>
     <th>Device</th>
     <td>Group1</td>
     <td>Group2</td>
  </tr>

Modified HTML :修改后的 HTML :

  <tr>
     <th>Device</th>
     <th>Serial</th>
     <th>ID</th>
     <th>Name</th>
     <th>Groups</th>
     <th>Device</th>
     <th>Group1</th>
     <th>Group2</th>
  </tr>

And the following code works:以下代码有效:

$("#tableid").on('click', 'td', function(e) {
   //debug statement to check if it entered the function
   console.log("debug: in function working") 

  // to get the column index of the cell clicked
  var index = $(this).index();

  // debug statement to check the index
  console.log(index); 

  // get the table
  var table = $(this).closest('table'); 

  // get the header name.
  var header = table.find('tr th').eq(index).text();
 
 //debug statement to check header name
 console.log(header); 

 //check if the header is group 1 then pop alert not to click button
  if(header == "Group1")
    alert("you cannot click button");  
});

Another thing you could do if the td is important is that you can put all header elements in tr with a class="header_validate" for example and change the line:如果 td 很重要,您可以做的另一件事是,您可以将所有标题元素放在 tr 中,例如 class="header_validate" 并更改行:

var header = table.find('tr th').eq(index).text();

with

var header = table.find('tr .header_validate').eq(index).text();

Just check for table names只需检查表名

let th = document.getElementsByTagName("th");
let td = document.getElementsByTagName("td");
let ts = [...th, ...td];
let isGroup = false;
ts.map(a => a.textContent.includes("Group1") && (isGroup = true));
console.log(isGroup);

First if there is no specific reason to use td tag like this (in 1st tr tag) then it can be used in next tr tag .首先,如果没有特定理由使用这样的td标签(在第一个tr标签中),那么它可以在下一个tr标签中使用。 Below solution is applicable on present use of tag but with some changes .下面的解决方案适用于目前使用的标签,但有一些变化。

You can loop through all th tag and match the innerHTML with required condition, if that matched get the i value (which is the tag having that value).您可以遍历所有th标签并将innerHTML与所需条件匹配,如果匹配得到i值(即具有该值的标签)。

Using i value print the output使用i值打印输出

 function func() { var trTag = document.querySelector("tr"); var thTag = trTag.querySelectorAll("th"); for (let i = 0; i < thTag.length; i++) { if (thTag[i].innerHTML === "Group1") var current = i; } document.querySelector("#demo").innerHTML = thTag[current].innerHTML; thTag[current].style.backgroundColor = "red" }
 <table class="table table-striped table-bordered" id="tableid"> <tbody> <tr> <th>Device</th> <th>Serial</th> <th>ID</th> <th>Name</th> <th>Groups</th> <th>Device</th> <th>Group1</th> <th>Group2</th> </tr> <tr> <td>TY83-FPSX-C3WS</td> <td>xxx1</td> <td>test1</td> <td>John</td> <td> Driver</td> <td>DKFU-V7ZE-RD9R</td> <td> <div class="col" classname="col"> <select name="group1" id="groupId"> <option>Null</option> <option value="IT">IT</option> <option value="Cleaning">Cleaning</option> <option value="Accountant">Accountant</option> </select> </div> </td> <td> <div class="col" classname="col"> <select name="group1" id="groupId"> <option>Null</option> <option value="IT">IT</option> <option value="Cleaning">Cleaning</option> <option value="Accountant">Accountant</option> </select> </div> </td> </tr> <tr> <td>4SB9-NR2D-742E</td> <td>xxx2</td> <td>test2</td> <td>Cena</td> <td>Telesales</td> <td>DqwdKFU-VqwdZE-RD9R</td> <td> <div class="col" classname="col"> <select name="group1" id="groupId"> <option>Null</option> <option value="IT">IT</option> <option value="Cleaning">Cleaning</option> <option value="Accountant">Accountant</option> </select> </div> </td> <td> <div class="col" classname="col"> <select name="group1" id="groupId"> <option>Null</option> <option value="IT">IT</option> <option value="Cleaning">Cleaning</option> <option value="Accountant">Accountant</option> </select> </div> </td> </tr> </tbody> </table> <button onclick="func()">Click</button> <div id="demo"></div>

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

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