如何从jQuery 的下拉列表中获取选定的文本(而不是选定的值)?
使用jQuery从下拉列表(选择框)中获取选定的文本
$("#dropdown").find(":selected").text();
$("#dropdown :selected").text();
$("#dropdown option:selected").text();
$("#dropdown").children(":selected").text();
Try:
$var = jQuery("#dropdownid option:selected").val();
alert ($var);
Or to get the text of the option, use text()
:
$var = jQuery("#dropdownid option:selected").text();
alert ($var);
More Info:
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
Select Text and selected value on dropdown/select change event in jQuery
$("#yourdropdownid").change(function() {
console.log($("option:selected", this).text()); //text
console.log($(this).val()); //value
})
$("#selectID option:selected").text();
Instead of #selectID
you can use any jQuery selector, like .selectClass
using class.
As mentioned in the documentation here.
The :selected selector works for <option> elements. It does not work for checkboxes or radio inputs; use :checked
for them.
.text() As per the documentation here.
Get the combined text contents of each element in the set of matched elements, including their descendants.
So you can take text from any HTML element using the .text()
method.
Refer the documentation for a deeper explanation.
$("select[id=yourDropdownid] option:selected").text()
This works fine
Use this
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;
Then you get your selected value and text inside selectedValue
and selectedText
.
Various ways
1. $("#myselect option:selected").text();
2. $("#myselect :selected").text();
3. $("#myselect").children(":selected").text();
4. $("#myselect").find(":selected").text();
$("#DropDownID").val()
will give the selected index value.
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());
});
If you already have the dropdownlist available in a variable, this is what works for me:
$("option:selected", myVar).text()
The other answers on this question helped me, but ultimately the jQuery forum thread $(this + "option:selected").attr("rel") option selected is not working in IE helped the most.
Update: fixed the above link
$("option:selected", $("#TipoRecorde")).text()
$("#yourdropdownid option:selected").text();
For multi-selects: