2011-05-24 49 views
1
<label><input type="checkbox" name="" /> Normal</label> 
<label><input type="checkbox" name="" checked="checked" /> Checked</label> 
<label><input type="checkbox" name="" disabled="disabled" /> Disabled</label> 

如果該複選框爲「已檢查」,則其標籤應該具有「已檢查」類,對於「已禁用」類也是如此。基於輸入條件將父類添加到父級

我想這一點,但似乎並沒有工作:

$("input[type=checkbox][checked]").each( 
    function() { 
     $(this).parent().addClass('checked'); 
    } 
); 

http://jsfiddle.net/eJfT6/

感謝您的幫助!

+0

.toggle(可以幫助這裏。 – 2011-05-24 10:28:22

回答

0

您可以測試:

$('input:checkbox').each(function(){ 

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

     $(this).parent().addClass('checked'); 
    } 

    if($(this).is(':disabled')){ 

     $(this).parent().addClass('disabled'); 
    } 
}); 
0

試試這個....

$("input[type=checkbox][checked]").each(
    function() { 

     $(this).parent().addClass('checked'); 

    } 
); 
// for disabled 
$("input[type=checkbox]").each(
    function() { 
     if($(this).attr('disabled')){ 
      alert("in side"); 
      $(this).parent().addClass('checked'); 
     } 

    } 
); 

您可以合併這兩個功能我已經seprated它只是你的理解。
http://jsfiddle.net/Htcjb/