2013-08-02 66 views
1

我有一個事件列表,用戶可以通過選擇一系列複選框來進行篩選。我一直在使用數據連接到每個事件(我硬編碼的真/假值簡化):JQuery選擇器動態匹配.data()數組

$(thisDiv).data({ 
    "Category": { 
     "Category1": true, 
     "Category2": false, 
     "Category3": true, 
     "Category4": true, 
     "Category5": false 
    }, 
    "Topic": { 
     "Topic1" : true, 
     "Topic2" : false, 
     "Topic3" : false, 
     "Topic4" : true, 
     "Topic5" : true 
    } 
}); 

當用戶選擇/取消選擇相應的複選框我想隱藏基於相應/演出活動值,例如,如果選擇了Category1複選框我想選擇其中Category1 == true的所有事件。 (這樣想,你想一個作者或流派來過濾書亞馬遜圖書搜索。)

我的問題是語法我使用來選擇匹配:

$(checkboxes).change(function() { 
    if ($(this).is(':checked')) { 
     var group = this.groupName; //an attribute I added to the checkbox that contains the key (e.g. "Category" or "Topic") 
     var identifier = this.id; //e.g. "Category1"  
     $(eventContainer).each(function() { 
      //alert($(this).data(group).identifier); //.data(group) alone returns object, but once I add .identifier it fails 
     if ($(this).data("Category").Category1 == true) { //works 
      //show/hide events based on other conditions 
     };   
     }); //end function 
    } 
}); //end function 

什麼是正確的語法選擇符合條件的項目?

回答

1

你需要做的

$(this).data(group)[identifier] 
+0

完美!感謝您及時的回覆。 – user2643491