javascript - bsmultiselect:如何获取用户选择的项目列表或数组?
问题描述
我偶然发现了一个项目的bsmultiselect 库。界面看起来不错,但似乎文档需要更多工作,因此我无法弄清楚如何返回用户选择的项目列表。如果有人至少能给我一个提示,我将不胜感激。
这是我现在的代码:
const instSel = document.getElementById('instrument-select');
let x = new xhr();
x.get("GET", "instruments.json", (rsp) => {
instr = JSON.parse(rsp)
for (i in instr) {
let optGr = document.createElement("optgroup");
optGr.setAttribute("label", instr[i]["name"]);
for (x in instr[i]["instruments"]) {
let opt = document.createElement("option");
opt.setAttribute('value', instr[i]["instruments"][x]);
opt.setAttribute('class', 'dropdown-item');
opt.textContent = instr[i]["instruments"][x];
optGr.appendChild(opt);
}
instSel.appendChild(optGr);
console.log(optGr);
}
bs = dashboardcode.BsMultiSelect("#instrument-select");
console.log(bs);
});
document.body.addEventListener('dblclick', () => {
console.log(bs.getPicks()); // this returns the html only
});
提前感谢您的帮助!
更新
管理通过使用e.target
事件收集数据。
instSel.onchange = (e) => {
// loop through all the items
for (let i = 0; i < e.target.length; i++;) {
if (e.target[i].selected) { // if the item is selected
console.log(e.target[i].textContent)
}
}
}
解决方案
推荐阅读
- docker - Docker 撰写健康检查选项
- python-3.x - 我们如何在 kv 文件中使用循环创建多行?(Python3,基维)
- sql-server - 根据列值抛出用户定义的错误消息
- javascript - 如何将对象推入阵列 Firebase
- security - oAuth 安全评估 - Google 指定的第三方评估员
- json - 如何在 kibana 中使用 bar vizualitation 将秒转换为小时?
- java - 无法以一种小方法在 Java 中参数化 MySQL 语句
- android - android 应用程序错误... java.lang.IllegalStateException: 没有活动
- shell - 如何为 sed 创建一个简单的正则表达式?
- sql - Select from_Json DF 转换结果 null