Get selected value of a dropdown's item using jQuery

后端 未结 30 1571
广开言路
广开言路 2020-11-22 06:48

How can I get the selected value of a dropdown box using jQuery?
I tried using

var value = $(\'#dropDownId\').val();

and



        
相关标签:
30条回答
  • 2020-11-22 07:13

    You can use any of these:

    $(document).on('change', 'select#dropDownId', function(){
    
                var value = $('select#dropDownId option:selected').text();
    
                //OR
    
                var value = $(this).val();
    
    });
    
    0 讨论(0)
  • 2020-11-22 07:15

    HTML:

    <select class="form-control" id="SecondSelect">
           <option>5<option>
           <option>10<option>
           <option>20<option>
           <option>30<option>
    </select>
    

    JavaScript:

    var value = $('#SecondSelect')[0].value;
    
    0 讨论(0)
  • 2020-11-22 07:16

    For single select dom elements, to get the currently selected value:

    $('#dropDownId').val();
    

    To get the currently selected text:

    $('#dropDownId :selected').text();
    
    0 讨论(0)
  • 2020-11-22 07:18

    Try this jQuery,

    $("#ddlid option:selected").text();
    

    or this javascript,

     var selID=document.getElementById("ddlid");
     var text=selID.options[selID.selectedIndex].text;
    

    If you need to access the value and not the text then try using val() method instead of text().

    Check out the below fiddle links.

    Demo1 | Demo2

    0 讨论(0)
  • 2020-11-22 07:19

    Try this:

     $('#dropDownId option').filter(':selected').text();     
    
     $('#dropDownId option').filter(':selected').val();
    
    0 讨论(0)
  • 2020-11-22 07:20

    This will alert the selected value. JQuery Code...

    $(document).ready(function () {
    
            $("#myDropDown").change(function (event) {
                alert("You have Selected  :: "+$(this).val());
            });
        });
    

    HTML Code...

    <select id="myDropDown">
            <option>Milk</option>
            <option>Egg</option>
            <option>Bread</option>
            <option>Fruits</option>
        </select>
    
    0 讨论(0)
提交回复
热议问题