2011-12-06 119 views
8

對於我的需要我用jQuery的每個輸入hasClass

$('#form :input').each(function(i) { 
    if (!$(this).hasClass('donot')) { 
     $(this).attr('disabled', 'disabled'); 
    } 
}); 

更好的方法不使用如果條件檢查,如果輸入有類「DONOT」?

感謝您的幫助......

克里斯

回答

8
$('#form input:not(.donot)').each(function(i) { 
    $(this).attr('disabled', 'disabled'); 
}); 

而且你去:-D

Docs for :not() selector


或者你也可以這樣做:

$('#form input').not('.donot').each(function(i) { 
    $(this).attr('disabled', 'disabled'); 
}); 

Docs for .not()

+0

優雅!謝謝 – Chris

+0

@ user1080344沒問題^ _ ^高興幫忙:-D – Neal

2

試試這個,你也甚至不需要每個循環來做到這一點。

$('#form input:not(.donot)').attr('disabled', 'disabled'); 
+0

'+ 1'哈!這是真的:-我甚至沒有想到這一點。 – Neal

+0

這是正確的答案。 – harpo