我有一个问题,我正在寻找允许正确操作结构选择的jQuery库,它看起来像这样:
<select name="name['index']['index']">
...
</select>
或
<select name="name['index'][]['index']">
...
</select>
通常使用库select2,但是当我想展开列表时,目前返回错误:
未捕获错误:语法错误,无法识别的表达式:标签[for = name [&#39; index&#39;] [&#39; index&#39;]]
所以我的问题是,你们中有谁知道一个可以应对这种结构的图书馆吗?
或者像select2这样的东西允许,我不知道,你们其中一个人知道该怎么做?
答案 0 :(得分:0)
我认为这就是你想要的:
const select = (filter, node = document.body) => {
if (typeof filter == "string") {
return select(x => x.getAttribute('name') == filter, node)
} else if (typeof filter == "function") {
const nodeIterator = document.createNodeIterator(node, NodeFilter["SHOW_ELEMENT"], {
acceptNode: node => filter(node) == true ? NodeFilter.FILTER_ACCEPT : null
}), list = []
let temp
while (temp = nodeIterator.nextNode()) {
list.push(temp)
}
return list
}
}
您可以这样使用它:
select('name['index'][]['index']')
这将检查name属性是否是您指定的字符串 您还可以添加第二个参数,以便仅选择特定元素的子项。
您也可以编写功能来检查是否符合您的要求,尝试自己:D希望它适合您。