如何从jQuery的下拉列表中获取所选文本(而不是所选值)?
当前回答
$("#yourdropdownid option:selected").text();
其他回答
这对我有用:
$('#yourdropdownid').find('option:selected').text();
jQuery版本:1.9.1
$(函数(){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>
对于所选项目的文本,请使用:
$('select[name="thegivenname"] option:selected').text();
对于所选项目的值,请使用:
$('select[name="thegivenname"] option:selected').val();
使用此
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;
然后在selectedValue和selectedText中获取所选值和文本。
$("#selectID option:selected").text();
您可以使用任何jQuery选择器来代替#selectID,例如.selectClass using class。
如本文文档中所述。
:selected选择器适用于<option>元素。它不适用于复选框或无线电输入;用途:已检查。
.text()根据此处的文档。
获取匹配元素集合中每个元素的组合文本内容,包括其后代。
因此,您可以使用.text()方法从任何HTML元素中获取文本。
有关详细说明,请参阅文档。