2016-11-11 198 views
-1

我有一組複選框。一個是條件[]。這個數組的最後一個複選框被命名爲「none」。這樣,如果用戶選擇此複選框,則應檢查所有其他複選框。選中/取消選中無複選框

我想弄清楚如何編寫我的if語句,以便當頁面加載和任何其他複選框被選中時,它需要確保沒有被檢查。反之亦然,如果沒有其他複選框被選中,那麼我需要確保檢查沒有的複選框。

請記住,這些是動態創建的,實際上並沒有在數據庫中。

回答

1

你的問題不是很清楚。

// you need to add a class('.otherCheckboxClass') to all the checkbox other than the none one. 

var allOtherChecked = true; 
// go trough all checkboxes other than the none one 
$(".otherCheckboxClass").each(function() { 
    // verify if one of them is not checked 
    if(!$(this).is(":checked")){ 
    // if one is checked, set var to false 
    allOtherChecked = false; 
    } 

    // if all of them are checked, add check to the none checkbox. 
    if(allOtherChecked == true){ 
    $('.noneCheckBox').prop("checked", true); 
    } 

});