2012-04-17 75 views
3

下面的代碼,我得到tr元素id屬性:獲得TR元素的ID,如果該複選框被選中

var IDs = []; 
$(".head").each(function(){ IDs.push(this.id); }); 
alert(IDs); 

這些tr元素都有複選框。

我想要的是,如果複選框被選中,那麼我有這些tr id。我需要檢查複選框tr ID :)

我該如何實現它?

+1

哥哥的工作!歡呼聲:) – 2012-04-17 11:07:40

+0

你的意思是說你的'.head'元素是複選框,並且你想獲得他們父'tr'的'id'? – 2012-04-17 11:08:35

+0

.head是tr的類和tr複選框 – snnlankrdsm 2012-04-17 11:09:13

回答

2

你需要用它來獲得勾選複選框的父ID ...

var IDs = []; 
    $(".head input:checked").each(function(){ IDs.push($(this).parent().attr("id")); }); 
    alert(IDs); 

這裏的工作的例子...

http://jsfiddle.net/uMfe3/

+0

花花公子,$('#'+ Ids).each('slow',function(){slide}();這不起作用可能是什麼問題? – snnlankrdsm 2012-04-17 11:17:09

+0

這是一個完全不同的問題 - 創建一個新問題併發布完整的代碼來概述問題,最好是鏈接到jsfiddle之類的東西來向我們展示。 – Archer 2012-04-17 11:41:00

0

var IDs = []; 
//iterate over your <tr> 
$(".head").each(function(){ 
    //if there is atleas a checked checkbox 
    if($('input:checkbox:checked', this).length > 0){ 
     //add the id of the <tr> 
     IDs.push(this.id); 
    } 
}); 

alert(IDs); 

,或者你可以做

$(".head input:checkbox:checked").each(function(){ 
    IDs.push(this.id); 
}); 
+0

IDs是空的哪裏出錯:S – snnlankrdsm 2012-04-17 11:10:23

+1

@MertMetin有一個錯字,我認爲(長度而不是長度) – 2012-04-17 11:12:24

+0

$('#'+ Ids).each('slow',function ) { })。向上滑動();這不起作用可能是什麼問題? - – snnlankrdsm 2012-04-17 11:26:42

1

你可以做到這一點是這樣的...

var Ids = $('.head:has(:checkbox:checked)') 
      .map(function() { return this.id }) 
      .get(); 

如果你想讓它具有jQuery的執行速度更快內部槓桿querySelectorAll(),您可以使用...

var Ids = $('.head').filter(function() { 
       return $(this).has('input[type="checkbox"]') && this.checked; 
      }); 

...得到一個包含複選框的.head元素的jQuery集合。在你接受比男人

相關問題