根据id获取表列的值

时间:2015-05-09 10:30:50

标签: javascript jquery html-table

我创建了一个表,其中我想要Name列中的所有值(隐藏)。我使用了下面的jquery代码,使用nth-child(4)来获取所有值,但问题是这个表来自另一个应用程序,所以如果表中插入另一个列,那么下面的代码将不起作用。我的列标题为name

任何人都可以告诉我任何基于类或ID获取列值的解决方案。

Als我们如何检查id是否存在或缺失,如果id丢失则应用以下逻辑



$('#content table tbody tr td:nth-child(4)').each(function() {
  console.log('seee:', $(this).text());
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="content">
  <table border="1">
    <thead>
      <tr>
        <th>Test1</th>
        <th>Test2</th>
        <th>Test3</th>
        <th id="name" style="display:none">Name</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>23</td>
        <td>54</td>
        <td>76</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>34</td>
        <td>54</td>
        <td>32</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>65</td>
        <td>78</td>
        <td>56</td>
        <td style="display:none">lessi</td>
      </tr>
      <tr>
        <td>34</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>32</td>
        <td>65</td>
        <td>76</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>54</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>56</td>
        <td>76</td>
        <td>87</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>65</td>
        <td>67</td>
        <td>65</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>76</td>
        <td>67</td>
        <td>56</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>76</td>
        <td>65</td>
        <td>54</td>
        <td style="display:none">messi</td>
      </tr>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以获取#name元素的索引,然后在nth-child选择器

中使用它

var ids = $('#name').index();
$('#content table tbody tr td:nth-child(' + (ids + 1) + ')').each(function() {
  console.log('seee:', $(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="content">
  <table border="1">
    <thead>
      <tr>
        <th>Test1</th>
        <th>Test2</th>
        <th>Test3</th>
        <th id="name" style="display:none">Name</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>23</td>
        <td>54</td>
        <td>76</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>34</td>
        <td>54</td>
        <td>32</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>65</td>
        <td>78</td>
        <td>56</td>
        <td style="display:none">lessi</td>
      </tr>
      <tr>
        <td>34</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>32</td>
        <td>65</td>
        <td>76</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>54</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>56</td>
        <td>76</td>
        <td>87</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>65</td>
        <td>67</td>
        <td>65</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>76</td>
        <td>67</td>
        <td>56</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>76</td>
        <td>65</td>
        <td>54</td>
        <td style="display:none">messi</td>
      </tr>
    </tbody>
  </table>
</div>

答案 1 :(得分:1)

您可以使用:last-child,假设在td字段之间添加了新的testX

或者,您可以找到name标题,获取其index(),然后从那里找到td值:

var nameTdIndex = $('#name').index() + 1;
$('#content td:nth-child(' + nameTdIndex + ')').each(function() {
  console.log('seee:', $(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="content">
  <table border="1">
    <thead>
      <tr>
        <th>Test1</th>
        <th>Test2</th>
        <th>Test3</th>
        <th id="name" style="display:none">Name</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>23</td>
        <td>54</td>
        <td>76</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>34</td>
        <td>54</td>
        <td>32</td>
        <td style="display:none">jacob</td>
      </tr>
      <tr>
        <td>65</td>
        <td>78</td>
        <td>56</td>
        <td style="display:none">lessi</td>
      </tr>
      <tr>
        <td>34</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>32</td>
        <td>65</td>
        <td>76</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>54</td>
        <td>65</td>
        <td>34</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>56</td>
        <td>76</td>
        <td>87</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>65</td>
        <td>67</td>
        <td>65</td>
        <td style="display:none">firoz</td>
      </tr>
      <tr>
        <td>76</td>
        <td>67</td>
        <td>56</td>
        <td style="display:none">messi</td>
      </tr>
      <tr>
        <td>76</td>
        <td>65</td>
        <td>54</td>
        <td style="display:none">messi</td>
      </tr>
    </tbody>
  </table>
</div>