我有以下的HTML获得独特的元素计数的jQuery
<input type="hidden" name="product_id" value = "1" class="product_id" />
<input type="hidden" name="product_id" value = "2" class="product_id" />
<input type="hidden" name="product_id" value = "5" class="product_id" />
<input type="hidden" name="product_id" value = "1" class="product_id" />
<input type="hidden" name="product_id" value = "2" class="product_id" />
<input type="hidden" name="product_id" value = "2" class="product_id" />
我怎样才能Unique
的count
的元素(元素的独特价值)?
这是可能的without looping
?
预期结果:
UNIQUE ID COUNT
-----------------
1 2
2 3
5 1
我曾尝试是像下面,但这个 方法的问题是,它会循环中的所有elements.As你可以看到,在 第一次迭代我们可以了解
product_id
的1
的计数,所以它需要跳过其他元素product_id
。我有 有一堆的元素,我不会循环所有的元素。
$(".product_id").each(function(){
//CHECK THE COUNT AND SAVE IT TO SOME ARRAY
});
更新:
它的好来downvote,但是如果你把评论 为下降投票将是非常有益的。我特别声明
without
循环,因为我 虽然有时它可能与jQuery的强大CSS 选择器。
如果不循环通过“”元素至少一次.... – sweetamylase 2013-03-26 19:05:03
@ ssilas777他们都没有相同的ID ... – Alnitak 2013-03-26 19:05:28
我只注意到它并删除了我的评论 – ssilas777 2013-03-26 19:05:59