簡體   English   中英

比較數組和dom元素檢查相同的ID

[英]comparing arrays to dom elements check for same IDs

我正在嘗試比較2個數組,以檢查數組中是否存在與ul中與我進行比較的元素相同的id。

我正在將一些傳入數據(在ajax調用成功后)縮減為僅一個id數組,如下所示

   var storeLibIds = data.libraries( function(obj){
            return obj.id;
    });

然后,我想將該數組與dom上的某些li進行比較,並且如果id都存在,則選中該元素內的復選框

所以我試着像這樣遍歷李

$.each(".lessonList li"), function(){

    //if $(this).attr("id") exists in storeLibIds, then $(this).find(".otherLibCheck").prop("checked", true); then check off the checkbox inside

        });

因此,我的基本想法是遍歷li並檢查其arrt(“ id”)與數組,如果存在,請選中該框。 我遇到的問題是我不確定是否要遍歷所有這些對象,或者不確定是否可以使用.filter檢查是否存在該復選框。 我很難理解這種邏輯,不勝感激。 謝謝!

一種方法是使用storeLibIds.indexOf(id)來檢查id是否存在於數組中。

$.each($(".lessonList li"), function(idx,obj){
      if (storeLibIds.indexOf(obj.attr("id")) != -1) {
           ...
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM