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


当前回答

只需添加以下行

$(this).prop('selected', true);

将.att替换为.prop,它适用于所有浏览器。

其他回答

只需尝试以下代码。

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

它返回所选选项的文本。

以下内容对我有用:

$.trim($('#dropdownId option:selected').html())

这对我来说很有用:

$("#city :selected").text();

我正在使用jQuery 1.10.2

$("#dropdownid").change(function(el) {
    console.log(el.value);
});

或者你可以用这个

$("#dropdownid").change(function() {
    console.log($(this).val());
});

使用此

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

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

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