我有一個特殊的問題,我正在處理。我會開始模糊,如果有人需要更多的細節,我可以提供有關該項目的背景知識。jQuery/javascript查找列表中的值
我具有選定的ID(從一個複選框選擇的):161
我有像這樣的ID的許多行:
["161", "165", "573", "190", "150", "283"] // this one would be it
["160", "311", "793", "309", "301"]
["161", "165", "395", "306"] // this one would be it
["160", "311", "668", "191", "216", "301"]
我需要識別出的ID的上面的行,哪些人的ID已被選中。這並不難,我可以遍歷每行ID(循環遍歷實際數組),並執行thisIDList[i] == selectedID
。當已經選擇了多個ID我有
問題是:["161", "306"]
現在我需要通過行循環和識別哪些行具有所選ID的兩個。
["161", "165", "573", "190", "150", "283"] // wouldn't be it
["160", "311", "793", "309", "301"]
["161", "165", "395", "306"] // this one would be it
["160", "311", "668", "191", "216", "301"]
等等。可以選擇1到5或6個ID:["161", "306", "216", "668"]
有人能指出我的方向嗎?我認爲這基本上是比較喜歡的兩個列表,其中A組名單所列每個項目需要在列表B.對發現
編輯
我要補充該行能夠包含在沒有發現其他ID選定的列表。所以,如果所選的ID是["161", "306"]
,然後["161", "165", "395", "306"]
將是一個匹配的是,儘管它包含了165和395.
編輯
要更新,給多一點信息。我有單選按鈕的列表:
<input type="checkbox" name="filter" value="301" />
<input type="checkbox" name="filter" value="161" />
<input type="checkbox" name="filter" value="573" />
<input type="checkbox" name="filter" value="190" />
我有一個無序列表,每個列表具有數據屬性(我使用所述元數據插件):
<ul>
<li data="{attrID: '160,197,161,194,195,190,162' }">Lorem Ipsum</li>
</ul>
當點擊單選按鈕:
// set the selected IDs
selectedIds = [];
$("input[name=filter]:checked").each(function(){
selectedIds.push(this.value);
});
// loop through each list
$('ul li').each(function() {
// get and set the metadata for the attrID key
meta = $(this).metadata();
idList = meta.attrID;
// find out if the selected IDs are found in this idList
var isMatch = matches(selectedIds,idList);
console.log(isMatch);
// my plan was to do this
if(isMatch){
// do something with this list item
}
});
請謹慎[支持IE中的`Array.indexOf()`](http://stellapower.net/content/javascript-support-and-arrayindexof-ie)... – Eric 2010-12-01 20:41:18
謝謝!現在試試這個... – jyoseph 2010-12-01 21:09:55