如何从jQuery的下拉列表中获取所选文本(而不是所选值)?
当前回答
这段代码对我有用。
$("#yourdropdownid").children("option").filter(":selected").text();
其他回答
这里张贴的答案,例如,
$('#yourdropdownid option:selected').text();
对我来说没用,但这样做了:
$('#yourdropdownid').find('option:selected').text();
它可能是jQuery的旧版本。
$('#id').find('option:selected').text();
$("#dropdownID").change(function(){
alert($('option:selected', $(this)).text());
});
对于所选项目的文本,请使用:
$('select[name="thegivenname"] option:selected').text();
对于所选项目的值,请使用:
$('select[name="thegivenname"] option:selected').val();
这对我有用
$("#dropdownid").change(function() {
alert($(this).find("option:selected").text());
});
如果元素是动态创建的
$(document).on("change", "#dropdownid", function() {
alert($(this).find("option:selected").text());
});