复选框始终会检查它们是否存在

时间:2012-12-29 23:14:10

标签: jquery

HTML:

<div id="appcheck">
  <input class="precheck" type="checkbox" /> 
  <input class="precheck" type="checkbox" />
  <input class="precheck" type="checkbox" />
</div>

应该发现未经检查结果的jQuery。无论检查了多少个盒子,总是返回3 not checked

$('input.precheck').each(function() {
  if($(this).not(':checked')) {
    console.log('not checked');
  }
});

1 个答案:

答案 0 :(得分:5)

您可以使用is +否定运算符代替notnot不返回布尔值;它返回一个jQuery对象,你的if语句总是为真。

if (!$(this).is(':checked')) {

或:

if (!this.checked) {

您也可以编码:

var notChecked = $('input.precheck').filter(function(){
   return !this.checked;
}).length;