There are a couple of things you need to change:
It looks like you are allowing the user to select multiple option
. For that, make sure multiple
attribute is assigned to your select
element.
Add an onchange
handler to your select
Filter all selected indexes
<select id="mySelect" onchange="handler()" multiple>
<option id="some-id" value="some-value"></option>
</select>
var filtered;
function handler() {
const selector = document.getElementById('mySelect');
const selected = Array.prototype.filter.call(selector.options, o => o.selected);
filtered = selected.map(s => s.value);
}
Every time you change an option (deselecting it, or adding one) the handler will reassign the array. This will save you the effort of keeping the array unique by not having to check the array every time to see if the value already exists or not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…