How to get selected value from Dropdown list in JavaScript

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

    Hope it's working for you

     function GetSelectedItem()
     {
         var index = document.getElementById(select1).selectedIndex;
    
         alert("value =" + document.getElementById(select1).value); // show selected value
         alert("text =" + document.getElementById(select1).options[index].text); // show selected text 
     }
    
    0 讨论(0)
  • 2020-12-28 16:37

    Here is a simple example to get the selected value of dropdown in javascript

    First we design the UI for dropdown

    <div class="col-xs-12">
    <select class="form-control" id="language">
        <option>---SELECT---</option>
        <option>JAVA</option>
        <option>C</option>
        <option>C++</option>
        <option>PERL</option>
    </select>
    

    Next we need to write script to get the selected item

    <script type="text/javascript">
    $(document).ready(function () {
        $('#language').change(function () {
            var doc = document.getElementById("language");
            alert("You selected " + doc.options[doc.selectedIndex].value);
        });
    });
    

    Now When change the dropdown the selected item will be alert.

    0 讨论(0)
  • 2020-12-28 16:37

    I would say change var sv = sel.options[sel.selectedIndex].value; to var sv = sel.options[sel.selectedIndex].text;

    It worked for me. Directing you to where I found my solution Getting the selected value dropdown jstl

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 16:53

    It is working fine with me.

    I have the following HTML:

    <div>
        <select id="select1">
            <option value="1">test1</option>
            <option value="2" selected="selected">test2</option>
            <option value="3">test3</option>
        </select>
        <br/>
        <button onClick="GetSelectedItem('select1');">Get Selected Item</button>
    </div>
    

    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.

    0 讨论(0)
  • 2020-12-28 16:55

    According to Html5 specs you should use -- element.options[e.selectedIndex].text

    e.g. if you have select box like below :

    <select id="selectbox1">
        <option value="1">First</option>
        <option value="2" selected="selected">Second</option>
        <option value="3">Third</option>
    </select>
    <br/>
    <button onClick="GetItemValue('selectbox1');">Get Item</button>
    

    you can get value using following script :

    <script>
     function GetItemValue(q) {
       var e = document.getElementById(q);
       var selValue = e.options[e.selectedIndex].text ;
       alert("Selected Value: "+selValue);
     }
    </script>
    

    Tried and tested.

    0 讨论(0)
提交回复
热议问题