简体   繁体   English

如何使用JQuery循环表元素和检索数据元素?

[英]How do I loop through table elements AND retrieve data elements using JQuery?

I have an html table that looks like this in a standard HTML 4.01 Transitional page: 我有一个在标准HTML 4.01 Transitional页面中看起来像这样的html表:

<table>
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>etc...</th>
    </tr>
  </thead>
  <tbody id="tableBodyID">
    <tr class="rowElement" data-element="some-data-here">
      <td>Some Table Data</td>
      <td>Some More Table data</td>
      <td>etc.</td>
    </tr>
    <tr class="rowElement" data-element="some-data-here">
      <td>Some Table Data</td>
      <td>Some More Table data</td>
      <td>etc.</td>
    </tr>
  </tbody>
</table>

I want to loop through all the rows and get the "data" in the data-element field and place it in a variable using JQuery. 我想循环遍历所有行并获取data-element字段中的“data”,并使用JQuery将其放入变量中。 I have tried a number of variations of .children(), .data(), and .each() but have not been able to retrieve the "data" elements. 我尝试过.children(),. data()和.each()的多种变体,但是无法检索“data”元素。

I have tried (inside a $(document).ready() block...): 我试过(在$(document).ready()块中...):

$('tbody > tr').each(function() {
  alert( $(this).data('element'));  // CORRECTION - this works.
});

$('tbody').children().each(function() {
  alert( $(this).data('element'));  // CORRECTION - this works
});

$('.rowElement').each(function(i, obj) {
  rowValue = $(this);
  alert(rowValue.data('element'));  // CORRECTION - this works
});

Any help is welcome. 欢迎任何帮助。 I am using HTML 4.01, and JQuery 1.7.1. 我使用的是HTML 4.01和JQuery 1.7.1。 I have been testing in Firefox, but need to support the other standard browser Firefox, Opera, Safari, Chrome, and IE8+. 我一直在Firefox中测试,但需要支持其他标准浏览器Firefox,Opera,Safari,Chrome和IE8 +。

(Edited for minor syntax changes) (针对较小的语法更改进行了编辑)

The actual problem was a case issue, please see comment below. 实际问题是一个案例问题,请参阅下面的评论。 Issue RESOLVED. 问题解决了。

This works for me: 这对我有用:

$('table').find('tbody > tr').each(function() {
  alert( $(this).data('element')); 
});

http://jsfiddle.net/LBKvm/ http://jsfiddle.net/LBKvm/

(it may not be the most efficient way however) (但它可能不是最有效的方式)

Using .map and .get , you can map each tr into the element data attribute, so as to get an array of those values. 使用.map.get ,可以将每个tr映射到element数据属性,以获取这些值的数组。 As for the selector, an ID is the most straight-forward since it points directly to the tbody element, of which you want the tr descendants: http://jsfiddle.net/zeFGW/ . 至于选择器,ID是最直接的,因为它直接指向tbody元素,你想要tr后代: http//jsfiddle.net/zeFGW/

var elementValues = $("#tableBodyID tr.rowElement").map(function() {
  return $(this).data("element"); // replace each `tr` with its `element` data
}).get(); // turn jQuery object into an array

Your logic/calls work for me, you have a few issues with syntax though, brackets after ('element'); 你的逻辑/调用对我有用,但你有一些语法问题,括号后面('element'); 's should be ('element')); 应该是('element')); and the selectors need to be quoted also, the tbody ones are missing should be ('tbody') etc. 并且选择器也需要引用,缺少的那些应该是('tbody')等。

The jsfiddle . jsfiddle (Note my replacement of alert to console.log calls) (注意我将alert更换为console.log调用)

Tested in chrome 17. 测试铬17。

Is your code different from what is posted in the question? 您的代码与问题中发布的代码有何不同?

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

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