Getting the selected values in a multiselect tag in Javascript

前端 未结 3 681
逝去的感伤
逝去的感伤 2021-02-07 09:35

I have the following code

function searchFlights() {
    var select1 = document.getElementById(\"airports-select-1\");
    var selected1 = [];
    while(select1         


        
3条回答
  •  自闭症患者
    2021-02-07 10:01

    Another approach for those who like a more functional style:

    selections = Array.from(selectBox.options).filter(o => o.selected).map(o => o.value)
    

    or

    selections = Array.from(selectBox.selectedOptions).map(o => o.value)
    

提交回复
热议问题