Getting the selected values in a multiselect tag in Javascript

前端 未结 3 635
逝去的感伤
逝去的感伤 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:18

    Wouldn't this do it:

    function searchFlights() {
        var select1 = document.getElementById("airports-select-1");
        var selected1 = [];
        for (var i = 0; i < select1.length; i++) {
            if (select1.options[i].selected) selected1.push(select1.options[i].value);
        }
        console.log(selected1);
    }​
    

    function searchFlights() {
        var select1 = document.getElementById("airports-select-1");
        var selected1 = [];
        for (var i = 0; i < select1.length; i++) {
            if (select1.options[i].selected) selected1.push(select1.options[i].value);
        }
        console.log(selected1);
    }

    jsFiddle example

提交回复
热议问题