jQuery 如何使用jQuery获取表格单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5612128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:28:59  来源:igfitidea点击:

How to get a table cell value using jQuery

jqueryhtml-tablecell

提问by BarclayVision

I am trying to get the td values using jQuery for a table I have no control over. There is an excellent example posted on this site, but it references one column not multiple ones within each tr.

我正在尝试使用 jQuery 为我无法控制的表获取 td 值。该站点上发布了一个很好的示例,但它引用了一个列,而不是每个 tr 中的多个列。

I also don't have an id for the table or rows... only a class=columndatafor the td.

我也没有表或行的 id ......只有class=columndatatd的 id 。

$('#mytable tr').each(function() {
    var customerId = $(this).find("td").eq(2).html();    
}

will get the single td of a fixed tr?

将获得固定 tr 的单个 td?

Here is my table data:

这是我的表数据:

<tr valign="top">
    <td class="columnaction" valign="center">
        <img src="../images/spacer.gif" width="1" height="1" />
        <a href="javascript:void(0);" class="columnactionlink" onclick="return doAccept('599577', '1', '','');" alt="Accept">Accept</a>
    </td>
    <td class="columndata">Message</td>
    <td class="columndata">Test</td>
    <td class="columndata"></td>
    <td class="columndata"></td>
    <td class="columndata">04/09/2011 23:59</td>
    <td class="columndata">04/09/2011</td>
    <td class="columndata">05/12/2011 07:00</td>
    <td class="columndata">05/13/2011 07:00</td>
    <td class="columndata"></td>
    <td class="columndata">Doe, Jeffrey A. (xx)</td>
    <td class="columndata">913014405580</td>
    <td class="columndata">Skip</td>
    <td class="columndata">04/09/2011 16:37</td>
    <td class="columndata">C</td>
    <td class="columndata">Doe,J</td>
    <td class="columndata">04/09/2011 16:37</td>
    <td class="columndata">04/09/2011 23:59</td>
    <td class="columndata">04/09/2011 16:34</td>
</tr>

Here is the reference to a similar question: How to get a table cell value using jQuery?

这是对类似问题的参考: How to get a table cell value using jQuery?

回答by danidacar

This is the selector you need:

这是您需要的选择器:

$('#mytable tbody tr td:nth-child(2)')

回答by Jeremy S.

As @danip pointed out int his statement you can directly access a TD via a valid CSS selector

正如@danip 在他的声明中指出的那样,您可以通过有效的 CSS 选择器直接访问 TD

$('#mytable tbody tr td:nth-child(2)')

a little description what he is doing here

稍微描述一下他在这里做什么

  1. he selects the table with id=mytable by addressing it with #mytable
  2. then he goes down to the tbody of the table
  3. he selects all trs of this table but futher defines
  4. to select all tds under each tr
  5. lastly he select from all these tds only the second on for each tr in the table!
  1. 他通过使用#mytable 寻址来选择带有 id=mytable 的表
  2. 然后他下到桌子的 tbody
  3. 他选择了这个表的所有 trs 但进一步定义
  4. 选择每个 tr 下的所有 tds
  5. 最后,他从所有这些 tds 中只为表中的每个 tr 选择第二个!

you can iterate over the returned dom elements via each like here, and grab the HTML or Textcontent via html() or ()text

您可以通过这里的 each 遍历返回的 dom 元素,并通过 html() 或 ()text 获取 HTML 或 Textcontent

$('#mytable tbody tr td:nth-child(2)').each(function() {
   output += $(this).text();
});

see this jsfiddle to try it out yourself

看到这个 jsfiddle 自己尝试一下

http://jsfiddle.net/SdBBy/

http://jsfiddle.net/SdBBy/

Regards

问候

回答by Simranjit Singh

Out of the context here but might help you: if you have more than 100 or so rows, the above code will be a bit slower, a 1000 rows and performance will be much slower, in that case you can go with:

脱离上下文但可能对您有所帮助:如果您有超过 100 行左右,上面的代码会慢一点,1000 行和性能会慢得多,在这种情况下,您可以使用:

$('td:nth-child(2)', '#mytable') //$(selector, context)

and a for loop where you should first cache this object like

和一个 for 循环,你应该首先缓存这个对象,比如

var items = $('td:nth-child(2)', '#mytable');
for(var i=0;i<items.length;i++)
{
    //your code
}

Regards, SJ

问候, SJ

回答by Anish Sharma

use .text()instead of .html().

使用.text()代替.html().