首页 > 解决方案 > 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)
        }
    }
}

标签: javascriptarrayslistarraylistbsmultiselect

解决方案


推荐阅读