如何从jQuery的下拉列表中获取所选文本(而不是所选值)?


当前回答

如果要将结果作为列表,请使用:

x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})

其他回答

只需添加以下行

$(this).prop('selected', true);

将.att替换为.prop,它适用于所有浏览器。

$("#dropdown").find(":selected").text();


$("#dropdown :selected").text();

$(“#下拉选项:selected”).text();

$("#dropdown").children(":selected").text();

用于获取所选值

$('#dropDownId').val();

要获取所选项目文本,请使用此行:

$("#dropDownId option:selected").text();
$("#dropdownid").change(function(el) {
    console.log(el.value);
});

或者你可以用这个

$("#dropdownid").change(function() {
    console.log($(this).val());
});

对于所选项目的文本,请使用:

$('select[name="thegivenname"] option:selected').text();

对于所选项目的值,请使用:

$('select[name="thegivenname"] option:selected').val();