【发布时间】:2010-12-01 19:36:15
【问题描述】:
我正在处理一个特殊的问题。我会开始含糊不清,如果有人需要更多详细信息,我可以提供该项目的背景。
我有一个选定的 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"]
现在我需要遍历行并确定哪些行具有 both 的选定 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
}
});
【问题讨论】:
标签: javascript jquery arrays list