简体   繁体   English

获取表中的tds值

[英]Get values of tds in a table

Ok how you can see in my fiddle im trying to manipulate the left table, so that at the end it looks like the table on the right: http://jsfiddle.net/SULK3/2/ 好的,你可以在我的小提琴中看到我试图操纵左表,以便最后看起来像右边的表: http//jsfiddle.net/SULK3/2/

To do this i first try to get the values of the td s loop through them and look for the dates and change the rowspans upon the data i get: So i started with a normal table and this code: 要做到这一点,我首先尝试通过它们获取td s循环的值,并查找日期并更改我得到的数据上的rowspans:所以我开始使用普通表和此代码:

var values = [];
   var table = $(this).closest("table");
   table.find("tr").each(function() {
      values.push($(this).find("td:first").html());
   });

$('#values').text(values); 

But somehow it wont work, the values of the rows wont be outgiven in the div with the id values! 但不知怎的,它不会工作,行的值不会在具有id值的div中被忽略! Why? 为什么? (If you have ideas how to solve my 'big'-problem please post it also here!) Thanks (如果你有想法如何解决我的'大'问题,请在这里发布!)谢谢

In this line 在这一行

var table = $(this).closest("table");

what is this ? 是什么this In the fiddle, it's the window, which has no closest table, as closest() means closest parent element? 在小提琴中,它是没有最近表的窗口,因为nearest()表示最接近的父元素?

As there is only one table , are two tables, just changing it to 因为只有一个表 ,所以只有两个表,只需将其更改为

var table = $('table:first')

seems to work : 似乎工作:

FIDDLE 小提琴

You are probably going to try to accomplish this with the array you are building. 您可能会尝试使用您正在构建的阵列来完成此任务。 And just wanted to know why the values were not showing up in #values . 而且只是想知道为什么价值没有出现在#values

I wasn't sure so I started working on a solution.. I came back to see you had already marked an accepted answer which is great that must have been what you were looking for. 我不确定所以我开始研究解决方案..我回来看你已经标记了一个接受的答案,这个答案很棒,一定是你要找的。

I don't expect or want you to mark this as the accepted answer but I had fun trying to find a solution, so here you go: 我不希望或希望您将此标记为已接受的答案,但我很乐意尝试找到解决方案,所以在这里你去:

var dateCount = 0;
var addtoTr = 0;
var table = $("table:first");
var date = table.find('tr:first').find('td:first').html();

   table.find("tr").each(function(index) {
       if(date === $(this).find("td:first").html() || date === ''){
        dateCount++;
       }
       else{
          table.find('tr').eq(addtoTr).prepend('<td rowspan='+dateCount+'>'+date+'</td>');
          dateCount = 1;
          date = $(this).find("td:first").html();
          addtoTr = index;
       } 
       $(this).find("td:first").remove();
   });
   table.find('tr').eq(addtoTr).prepend('<td rowspan='+dateCount+'>'+date+'</td>');

http://jsfiddle.net/SULK3/6/ http://jsfiddle.net/SULK3/6/

This is only one way to do it.. I'm sure there are many other, and better ways. 这只是一种方法。我相信还有很多其他的,更好的方法。

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

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