如何从jQuery的下拉列表中获取所选文本(而不是所选值)?


当前回答

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

如果您使用asp.net并编写

<Asp:dropdownlist id="ddl" runat="Server" />

那么你应该使用

$('#<%=ddl.Clientid%> option:selected').text();

其他回答

var someName = "Test";

$("#<%= ddltest.ClientID %>").each(function () {
    $('option', this).each(function () {
        if ($(this).text().toLowerCase() == someName) {
            $(this).attr('selected', 'selected')
        };
    });
});

这将帮助你找到正确的方向。如果您需要进一步的帮助,请告诉我,上面的代码经过了充分测试。

如果变量中已经有下拉列表,这对我很有用:

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

这个问题的其他答案对我有帮助,但最终jQuery论坛线程$(this+“option:selected”).attr(“rel”)选项在IE中不起作用帮助最大。

更新:修复了上述链接

$(函数(){alert('.val()='+$('#selectnumber').val()+'AND html()='+$('#selectnumber选项:selected').html()+'AND.text()=‘+$(‘#selectnumber选项:select').text(());});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><html xmlns=“http://www.w3.org/1999/xhtml"><head runat=“server”><title></title></head><body><form id=“form1”runat=“server”><div><select id=“selectnumber”><option value=“1”>一个</option><option value=“2”>两个</option><option value=“3”>三个</option><option value=“4”>四个</option></选择></div></form></body></html>

如果要将结果作为列表,请使用:

x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})

使用此

const select = document.getElementById("yourSelectId");

const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;   

然后在selectedValue和selectedText中获取所选值和文本。