2016-12-27 31 views
0

只選擇具有設置爲checked="checked"的屬性的選項,但選中全部。如果條件選中所有複選框

<input class="chxbx" type="checkbox" checked="checked"> 
<input class="chxbx" type="checkbox"> 
<input class="chxbx" type="checkbox"> 

的jQuery:

$(".chxbx").each(function(i, e){ 
    if($(".chxbx").prop("checked", true)){ 
    // this should select only the the input tag with 
    //checked="checked" but it selects all checkboxes 
    }  
}) 
+1

問題是什麼?我認爲你需要'$(「。chxbx:checked」)'參見[':checked' Selector](https://api.jquery.com/checked-selector/) – Satpal

+0

嗨Satpal,如果條件選擇所有複選框。在html中,只有第一個輸入標記被檢查,如果條件需要顯示只有第一個標記已被檢查,那麼jquery會被檢查 – Deke

回答

3

您沒有使用正確.each

應該是:

$(".chxbx").each(function(){ 

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

    // this should select only the the input tag with 
    //checked="checked" but it selects all checkboxes 
    }  
}); 

或者:

$(".chxbx:checked").each(function(){ 

// this should select only the the input tag with 
//checked="checked" but it selects all checkboxes 
}); 
相關問題