2013-02-25 39 views
0

我需要執行以下操作:檢查tr的第3個td是否包含(完全)56,然後檢索包含在第一個td中的複選框的ID行。jquery檢索某些元素的ID並將它們寫入數組

<table> 
    <tr class="bg"> 
     <td nowrap="nowrap"> 
      <input class="selected_ads" type="checkbox" id="43617" /> 
     </td> 
     <td class="pa">text text</td> 
     <td class="pa">56</td> 
    </tr> 
    <tr class="bgwhite"> 
     <td nowrap="nowrap"> 
      <input class="selected_ads" type="checkbox" id="183578" /> 
     </td> 
     <td class="pa">text</td> 
     <td class="pa">56</td> 
    </tr> 
</table> 

($(。」 BG TD:第n個孩子(3):含有('56' ) 「)長度> 0)或($(」 TD bgwhite:第n個孩子(3) :contains('56')「)。length> 0)檢查第三個單元格是否包含我正在查找的值。 (「。pa」)。siblings()。first()。children(「input [type ='checkbox']」) 讓我複選框,但我無法檢索其ID。

理想的情況下我的代碼是這樣的:

var longlist = []; 
for each ($(".bg td:nth-child(3):contains('56')").length>0) { 

retrieve the id of $(".pa").siblings().first().children("input[type='checkbox']"); 
longlist.push(checkbox_id); 
} 

爲.bgwhite這樣做;

理想情況下它也可以工作。

對我來說最重要的是檢索id。

+0

很有可能您在每次迭代中選擇第一個值。你有沒有使用'.each'方法? – 2013-02-25 16:08:27

回答

0
$('.bg .pa:last').each(function(){ 

    if($(this).text() === '56'){ 

     longlist.push($(this) 
          .closest('.bg') 
          .find('.selected_ads') 
          .attr('id')); 
    } 
}); 
+0

謝謝。回到更多。每個 – flish 2013-02-25 18:27:40

+0

@flish Np,讓我知道如果你有任何問題 – Johan 2013-02-25 19:47:15

1

如果提供的jQuery元件:

var $foo = $(".pa").siblings().first().children("input[type='checkbox']"); 

至少有4種方式來訪問它的ID:

  1. var id = $foo[0].id; - 陣列解引用+ vanilla DOM
  2. var id = $foo.get(0).id; - http://api.jquery.com/get + vanilla DOM
  3. var id = $foo.attr('id'); - http://api.jquery.com/attr
  4. var id = $foo.prop('id'); - http://api.jquery.com/prop

你是說,你嘗試過所有這些,沒有工作?

+0

不,我在說,雖然我明白我在找什麼,但學習任何語言的語法對我來說都是痛苦的緩慢。但我正在學習。 – flish 2013-02-25 18:24:24

相關問題