2014-04-05 58 views
0

我有我的複選框的樣式標籤並隱藏了實際的複選框。檢查複選框點擊標籤時的狀態

<fieldset class='checkboxgroup'> 
    <div> 
     <input type='checkbox' id='item1' class='groupChkBox'> 
     <label for="item1">Julienne Carrots</label> 
    </div> 
    <div> 
     <input type='checkbox' id='item2' class='groupChkBox'> 
     <label for="item2">Lettuce Salad</label> 
    </div> 
</fieldset> 

$(".checkboxgroup label").click(function(){ 

    if ($(this).is(':checked')) { 
     $("#boxofnouse").append("<div class='"+$(this).attr('id')+"'>hello world "+$(this).attr('id')+"</div>") 
    } 

});

現在我的問題是如何檢查單擊標籤時是否選中複選框?

我試過使用,但沒有奏效。

var closestParentfieldset = $(this).closest("fieldset"); 
if ($(closestParentfieldset > input).is(':checked')) 

回答

2

您需要檢查是否選中複選框,不標註:

$(this).prev().is(':checked') 

Example

+0

這是+1在線基於CSS例如:檢查狀態:http://codepen.io/anon/pen/vhgqj/ –

0

試試這個..

$(this).siblings("input").is(":checked")