简体   繁体   English

如何使用jQuery从具有特定类的单行中获取表单元格的内容

[英]How to get the contents of a table cell from a single row with a specific class using jquery

I have a table that looks like this. 我有一张看起来像这样的桌子。

<table id="translations-table" class="table table-condensed clickable-table">
    <thead style="border-top: none;border-bottom: none;background-color: lightgrey;">
        <tr>
            <th class="hidden">MessageID </th>
            <th class="hidden">TextID </th>
            <th class="hidden">LanguageID</th>
            <th class="hidden">RealKeyword</th>
            <th>Keyword </th>
            <th>Language </th>
            <th>Text </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="hidden">27</td>
            <td class="hidden">27</td>
            <td class="hidden">1</td>
            <td class="hidden">APIERROR</td>
            <td>APIERROR </td>
            <td class="col-md-2">English (US)</td>
            <td>API error: Missing required parameter: ~parm~</td>
        </tr>
        <td class="hidden">27</td>
        <td class="hidden">808</td>
        <td class="hidden">3</td>
        <td class="hidden">APIERROR</td>
        <td> </td>
        <td class="col-md-2">French</td>
        <td>&lt;Enter translated text here.&gt;</td>
        </tr>
        <td class="hidden">87</td>
        <td class="hidden">86</td>
        <td class="hidden">1</td>
        <td class="hidden">AUTOINSTALL</td>
        <td>AUTOINSTALL </td>
        <td class="col-md-2">English (US)</td>
        <td>Unknown removed curtain; automatically installed</td>
        </tr>
    </tbody>
</table>

When a user clicks on a row in the table, it is highlighted with a colored background by adding class row-highlight to it. 当用户单击表中的一行时,通过向其添加类高亮显示,它以彩色背景突出显示。 Only 1 row can be highlighted at any point. 任何时候只能突出显示一行。

I have an Edit button which, when clicked, needs to get the values of various cells within the highlighted row. 我有一个“编辑”按钮,单击该按钮需要获取突出显示的行中各个单元格的值。 I've tried several jQuery selectors to do this, for example: 我尝试了几个jQuery选择器来做到这一点,例如:

var messageid=$("#translations-table").find("tbody tr .row-highlight td[0]").text();

Nothing I've tried works, they all return empty in messageid, as shown with an alert statement. 我没有尝试过任何工作,它们都在messageid中返回空,如警报语句所示。

Any help much appreciated. 任何帮助,不胜感激。

Your html is invalid. 您的html无效。 Bellow is the valid version of your html, last row has class row-highlight. 波纹管是您html的有效版本,最后一行具有row-highlight类。 You can select text value of the first td element with this code: 您可以使用以下代码选择第一个td元素的文本值:

var messageid = $("#translations-table").find("tbody tr.row-highlight td:nth-child(1)").text();

<table id="translations-table" class="table table-condensed clickable-table">
  <thead style="border-top: none;border-bottom: none;background-color: lightgrey;">
    <tr>
      <th class="hidden">MessageID </th>
      <th class="hidden">TextID </th>
      <th class="hidden">LanguageID</th>
      <th class="hidden">RealKeyword</th>
      <th>Keyword </th>
      <th>Language </th>
      <th>Text </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="hidden">27</td>
      <td class="hidden">27</td>
      <td class="hidden">1</td>
      <td class="hidden">APIERROR</td>
      <td>APIERROR </td>
      <td class="col-md-2">English (US)</td>
      <td>API error: Missing required parameter: ~parm~</td>
    </tr>
    <tr>
      <td class="hidden">27</td>
      <td class="hidden">808</td>
      <td class="hidden">3</td>
      <td class="hidden">APIERROR</td>
      <td> </td>
      <td class="col-md-2">French</td>
      <td>&lt;Enter translated text here.&gt;</td>
    </tr>
    <tr class='row-highlight'>
      <td class="hidden">87</td>
      <td class="hidden">86</td>
      <td class="hidden">1</td>
      <td class="hidden">AUTOINSTALL</td>
      <td>AUTOINSTALL </td>
      <td class="col-md-2">English (US)</td>
      <td>Unknown removed curtain; automatically installed</td>
    </tr>
  </tbody>
</table>

由于该类永远只有一个条目,因此您可以直接使用该类:

$(".row-highlight").children()[0].innerText

Here's a simple fiddle that demonstrates how to get cell values from your table: https://jsfiddle.net/Lar5j3a1/ 这是一个简单的小提琴,演示了如何从表中获取单元格值: https : //jsfiddle.net/Lar5j3a1/

HTML (with rows fixed as explained above):

    <table id="translations-table" class="table table-condensed clickable-table">
      <thead  style="border-top: none;border-bottom: none;background-color: lightgrey;">
        <tr>
           <th class="hidden">MessageID </th>
           <th class="hidden">TextID </th>
           <th class="hidden">LanguageID</th>
           <th class="hidden">RealKeyword</th>
           <th>Keyword </th>
           <th>Language </th>
           <th>Text </th>
         </tr>
       </thead>
       <tbody>
         <tr>
           <td class="hidden">27</td>
           <td class="hidden">27</td>
           <td class="hidden">1</td>
           <td class="hidden">APIERROR</td>
           <td>APIERROR </td>
           <td class="col-md-2">English (US)</td>
           <td>API error: Missing required parameter: ~parm~</td>
         </tr>
         <tr>
           <td class="hidden">27</td>
           <td class="hidden">808</td>
           <td class="hidden">3</td>
           <td class="hidden">APIERROR</td>
           <td> </td>
           <td class="col-md-2">French</td>
           <td>&lt;Enter translated text here.&gt;</td>
          </tr>
          <tr>
            <td class="hidden">87</td>
            <td class="hidden">86</td>
            <td class="hidden">1</td>
            <td class="hidden">AUTOINSTALL</td>
            <td>AUTOINSTALL </td>
            <td class="col-md-2">English (US)</td>
            <td>Unknown removed curtain; automatically installed</td>
          </tr>
        </tbody>
    </table>

jQuery: jQuery的:

$(document).ready(function(){
    $("#translations-table > tbody > tr").each(function(){
        var firstTD = $(this).find("td:nth-child(1)");
        alert(firstTD.text())       
    });
});

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

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