如何通过jQuery检查一些复选框的属性值?

时间:2012-02-16 21:52:50

标签: jquery

我需要一些帮助,我有一些像这样的复选框

<input class="some-class" type="checkbox" has-format="0">

<input class="some-class" type="checkbox" has-format="1">

是否可以检查所有选中的复选框是否has-format值= 1或0?我的意思是,例如如果我想检查= 1,那么如果任何选中的复选框具有该值0,那么我将得到假

2 个答案:

答案 0 :(得分:2)

您可以将:checked选择器与属性选择器结合使用。如果满足所有指定条件,则以下变量仅为true

var checked_and_format1 = !$('.some-class:checked[has-format!=1]').length;
// Selects:
//  .some-class 
//  which is checked
// and has a `has-format` property which is not 1.
// If the collection's size is zero, then we can assume that the whole
//  .some-class:checked collection is valid.

如果你确定除了1和0之外没有其他值,你可以用[has-format!=1]替换[has-format=0]

答案 1 :(得分:1)

var isValid = $('.some-class:checked[has-format="0"]').length == 0;