2013-03-19 32 views
2

我有一張表,少數行的類別爲SomeClass。 對於少數那些行(有類),我爲.data("MyKey")存儲每行不同的值。 我需要的行數爲SomeClass.data("MyKey")的行數爲MyValue獲取具有特定.data()值的行數

我想是這樣的:

$("#tbl tr.SomeClass").data('MyKey') == "MyValue" //.length 

但我知道這是不對的。

回答

3

您可以使用filter方法:

var count = $('#tbl tr.SomeClass').filter(function(){ 
     return $(this).data('MyKey') === 'MyValue'; 
}).length; 

或者Attribute Equals選擇:

$("#tbl tr.SomeClass").filter('[data-MyKey="MyValue"]').length;