如何从jQuery的下拉列表中获取所选文本(而不是所选值)?
当前回答
对于多选:
$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
其他回答
这里张贴的答案,例如,
$('#yourdropdownid option:selected').text();
对我来说没用,但这样做了:
$('#yourdropdownid').find('option:selected').text();
它可能是jQuery的旧版本。
使用此
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;
然后在selectedValue和selectedText中获取所选值和文本。
以下内容对我有用:
$.trim($('#dropdownId option:selected').html())
只需尝试以下代码。
var text= $('#yourslectbox').find(":selected").text();
它返回所选选项的文本。
试试看:
$("#myselect :selected").text();
对于ASP.NET下拉列表,可以使用以下选择器:
$("[id*='MyDropDownId'] :selected")