How to get selected value from Dropdown list in JavaScript

前端 未结 6 956
無奈伤痛
無奈伤痛 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:53

    It is working fine with me.

    I have the following HTML:


    And the following JavaScript:

    function GetSelectedItem(el)
    {
        var e = document.getElementById(el);
        var strSel = "The Value is: " + e.options[e.selectedIndex].value + " and text is: " + e.options[e.selectedIndex].text;
        alert(strSel);
    }
    

    See that you are using the right id. In case you are using it with ASP.NET, the id changes when rendered.

提交回复
热议问题