2013-07-20 81 views
-2
<input type="checkbox" class="africa" id="Senegal"/>Senegal 
<input type="checkbox" class="africa" id="Camerun"/>Camerun 
<input type="checkbox" class="europe" id="Spain"/>Spain 
<input type="checkbox" class="asia" id="Japan"/>Japan 
<input type="checkbox" class="europe" id="Germany"/>Germany 

我有一些复选框,如何选择不是非类的复选框?用JQuery选择复选框ID

结果应该是西班牙,日本,德国。

$('input:checkbox').not('.africa').each(function() { 
    alert($(this).attr('id')) 
}) 

回答

1

使用not method试试这个jQuery代码

var everythingElse = $('input[type=checkbox]').not('.africa'); 
+0

使用jQuery的选择扩展并不是最好的做法。 –

+0

现在好吗? –

+2

@ Lucas:没有。[请阅读文档中的原因](http://api.jquery.com/checkbox-selector/):*“因为':checkbox'是一个jQuery扩展,而不是CSS规范的一部分,查询使用':checkbox'不能利用原生DOM'querySelectorAll()'方法提供的性能提升,为了在现代浏览器中获得更好的性能,请使用'[type =“checkbox”]'“*(引号围绕'虽然checkbox是不必要的。) –

0

另一种方式来做到这一点:

$('input[type=checkbox]:not(.africa)').each(function() { 
    console.log($(this).attr('id')); 
}); 
0

尝试这jQuery代码 -

$(document).ready(function(){ 
    $('input[type=checkbox]').click(function(){ 
     if($(this).is(':checked')){ 
      alert($(this).attr('id')); 
     } 
    }); 
}); 

Try This