Get selected text from a drop-down list (select box) using jQuery

前端 未结 30 1684
南方客
南方客 2020-11-21 15:58

How can I get the selected text (not the selected value) from a drop-down list in jQuery?

相关标签:
30条回答
  • 2020-11-21 16:22

    This works for me

    $("#dropdownid").change(function() {
        alert($(this).find("option:selected").text());
    });
    

    If the element created dynamically

    $(document).on("change", "#dropdownid", function() {
        alert($(this).find("option:selected").text());
    });
    
    0 讨论(0)
  • 2020-11-21 16:24

    The answers posted here, for example,

    $('#yourdropdownid option:selected').text();
    

    didn't work for me, but this did:

    $('#yourdropdownid').find('option:selected').text();
    

    It is possibly an older version of jQuery.

    0 讨论(0)
  • 2020-11-21 16:24

    Use:

    ('#yourdropdownid').find(':selected').text();
    
    0 讨论(0)
  • 2020-11-21 16:24

    the following worked for me:

    $.trim($('#dropdownId option:selected').html())
    
    0 讨论(0)
  • 2020-11-21 16:28

    If you already have the dropdownlist available in a variable, this is what works for me:

    $("option:selected", myVar).text()
    

    The other answers on this question helped me, but ultimately the jQuery forum thread $(this + "option:selected").attr("rel") option selected is not working in IE helped the most.

    Update: fixed the above link

    0 讨论(0)
  • 2020-11-21 16:28

    $("#DropDownID").val() will give the selected index value.

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