Get selected text from a drop-down list (select bo

2018-12-31 02:32发布

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

30条回答
宁负流年不负卿
2楼-- · 2018-12-31 03:30

For the text of the selected item, use:

$('select[name="thegivenname"] option:selected').text();

For the value of the selected item, use:

$('select[name="thegivenname"] option:selected').val();
查看更多
临风纵饮
3楼-- · 2018-12-31 03:31
$("#yourdropdownid option:selected").text();
查看更多
倾城一夜雪
4楼-- · 2018-12-31 03:32

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());
});
查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 03:33
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.

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 03:34

Please use this

var listbox = document.getElementById("yourdropdownid");
var selIndex = listbox.selectedIndex;
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;   

Then Please alert "selValue" and "selText". You get your selected dropdown value and text

查看更多
浪荡孟婆
7楼-- · 2018-12-31 03:34

the following worked for me:

$.trim($('#dropdownId option:selected').html())
查看更多
登录 后发表回答