How to get selected value from Dropdown list in JavaScript

前端 未结 6 928
無奈伤痛
無奈伤痛 2020-12-28 16:17

How can you get the selected value from drop down list using JavaScript? I have tried the following but it does not work.

var sel = document.getElementById(\         


        
6条回答
  •  天涯浪人
    2020-12-28 16:39

    Direct value should work just fine:

    var sv = sel.value;
    alert(sv);
    

    The only reason your code might fail is when there is no item selected, then the selectedIndex returns -1 and the code breaks.

提交回复
热议问题