如何从jQuery的下拉列表中获取所选文本(而不是所选值)?
当前回答
在jQuery中的下拉列表/选择更改事件上选择文本和所选值
$("#yourdropdownid").change(function() {
console.log($("option:selected", this).text()); //text
console.log($(this).val()); //value
})
其他回答
使用此
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;
然后在selectedValue和selectedText中获取所选值和文本。
$("#dropdownID").change(function(){
alert($('option:selected', $(this)).text());
});
这对我有用
$("#dropdownid").change(function() {
alert($(this).find("option:selected").text());
});
如果元素是动态创建的
$(document).on("change", "#dropdownid", function() {
alert($(this).find("option:selected").text());
});
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
$(“#DropDownID”).val()将给出选定的索引值。