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

前端 未结 30 1728
南方客
南方客 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:10

    Use this

    const select = document.getElementById("yourSelectId");
    
    const selectedIndex = select.selectedIndex;
    const selectedValue = select.value;
    const selectedText = select.options[selectedIndex].text;   
    

    Then you get your selected value and text inside selectedValue and selectedText.

    0 讨论(0)
  • 2020-11-21 16:11
    $("option:selected", $("#TipoRecorde")).text()
    
    0 讨论(0)
  • 2020-11-21 16:13

    This code worked for me.

    $("#yourdropdownid").children("option").filter(":selected").text();
    
    0 讨论(0)
  • 2020-11-21 16:14

    Various ways

    1. $("#myselect option:selected").text();
    
    2. $("#myselect :selected").text();
    
    3. $("#myselect").children(":selected").text();
    
    4. $("#myselect").find(":selected").text();
    
    0 讨论(0)
  • 2020-11-21 16:15
    var someName = "Test";
    
    $("#<%= ddltest.ClientID %>").each(function () {
        $('option', this).each(function () {
            if ($(this).text().toLowerCase() == someName) {
                $(this).attr('selected', 'selected')
            };
        });
    });
    

    That will help you to get right direction. Above code is fully tested if you need further help let me know.

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

    Simply try the following code.

    var text= $('#yourslectbox').find(":selected").text();
    

    it returns the text of the selected option.

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