计算另一个选择的子集内的td的长度

时间:2012-08-02 17:30:04

标签: jquery html-table

我目前对我正在进行的项目感到难过。使用jQuery,我试图计算打开行中每个div对象的长度(tds的数量)。我有一个演示链接。

http://staging.mc.vanderbilt.edu/criss/ui-projects/r4u/073112/lab.html

点击“患者姓名”将打开该行,每个“药丸”将展开以显示数据。一些“pils”(。结果)有多个数据条目。我的jQuery选择应该计算每个.results中的tds,然后根据是否有2或3或更多的“.ear”显示允许扩展。如果tr.dates行中只有2个tds,我不希望耳朵显示。

如果在打开行之前单击“药丸”,我可以使用此功能。在患者一行中尝试“SPEP”。

我对Jquery比较陌生,因此在编写复杂函数或选择时并不擅长。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

要计算tr.dates中的tds数,您可以使用类似下面的内容

$(this).find('tr.dates td').length

假设$(this)将是当前的“.results”div。

因此,根据您的要求,如果上面的代码等于< = 2,则不显示“.ear”。

显示工作内容的更多代码

//Clicked on some button in tr with the class 'pt_row'
$('.pt_row td .name, .info_row td .name').click(function() {
   //Get closest pt_row and iterate through results section in this pt_row.
   $(this).closest('.pt_row').find('.results').each(function() {
      //Count number of tds in the dates
      if ( $(this).find('tr.dates td').length <= 2 ) {
         //We have 2 or less tds. So, don't show ear
         $(this).find('.ear').hide();
      } else {
         //We have more than 2 tds. So, show ear
         $(this).find('.ear').show();
      }
   });
});