简体   繁体   English

如何遍历HTML表列并使用JQuery将值输入到Javascript数组中

[英]How to iterate through an HTML table column and input the values into a Javascript array using JQuery

Let's say I have a table column with 10 rows, each with <td id="num"> and a text value. 假设我有一个包含10行的表列,每行包含<td id="num">和一个文本值。

How can I use JQuery to loop through each row in the column and input the spins into a Javascript array? 如何使用JQuery循环遍历列中的每一行并将旋转输入到Javascript数组中?

I thought the following code would do it, but it is only getting the first element: 我认为以下代码会这样做,但它只获得第一个元素:

var numArray = [];
$("#num").each(function(n){
   numArray[n] = $(this).text();
});

Any ideas? 有任何想法吗?

Thanks! 谢谢!

You can't have multiple elements with the same id. 您不能拥有具有相同ID的多个元素。 This isn't allowed because the id is used to identify individual elements in the DOM. 这是不允许的,因为id用于标识DOM中的各个元素。 I'd suggest giving them all the same class, which is allowed. 我建议给他们所有相同的课程,这是允许的。

<td class="num">

Then this should work: 那应该工作:

var numArray = [];
$(".num").each(function(n){
   numArray[n] = $(this).text();
});

Like mcos said, selecting by id for all the tables doesn't work. 像mcos说的那样,通过id为所有表选择都不起作用。 There can only be one item on a page with a given id. 页面上只能有一个具有给定ID的项目。

You can either give your table an id and do the following: 您可以为表提供ID并执行以下操作:

var numArray = [];
// Assuming #my-table-id is your table and you want all the tds
$("#my-table-id td").each(function(n){
   numArray[n] = $(this).text();
});

Or if you don't want all the tds, use a class to identify the ones you want 或者,如果您不想要所有tds,请使用类来标识您想要的那些

var numArray = [];
// Assuming #my-table-id is your table and you added class="collect" 
// to the tds you want to collect
$("#my-table-id td.collect").each(function(n){
   numArray[n] = $(this).text();
});

Also stealing from others answers, the map function can also help you make your code even smaller 此外,地图功能还可以帮助您将代码缩小

var numArray = $.map( $("#my-table-id td.collect"), function (td){
   return $(td).text();
})

it's advised that use don't reuse the ID but since it'll html.. it'll still work.. 建议使用不要重复使用ID,但因为它会html ..它仍然可以工作..

the jQuery ID(#) selector will only select the first match... jQuery ID(#)选择器只会选择第一个匹配...

you can use the td[id^='num'] or td[id*='num'] or td[id$='num'] instead 您可以使用td[id^='num']td[id*='num']td[id$='num']代替

use the map .. 使用map ..

var numArray = $("td[id^='num']").map(function(){
return $(this).text();
}).get();

This will select all the td's with ID's starting as num 这将选择ID为num所有td

See it here 在这里看到它

You can achieve the this with using .text(function(i, text){}) 您可以使用.text(function(i, text){})来实现这一点

var allText = [];
$("table td").text(function(i, t){
    allText.push(t);
});

Code example on jsfiddle 关于jsfiddle的代码示例

If you need to target a particular cell(s) you can just modify the selector. 如果您需要定位特定单元格,则只需修改选择器即可。

$("table td#num").text(function(i, text){
    allText.push(text);
});

With that being said, an id should be unique per dom and if you can adjust the html using a class would be the right way. 话虽如此,id应该是每个dom唯一的,如果你可以使用类调整html将是正确的方法。

 <td class="num">
   some text 1
 </td>

$("table td.num").text(function(i, text){
    allText.push(text);
});

Example

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

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