如何使用jQuery获得下拉框的选定值? 我试着用

var value = $('#dropDownId').val();

and

var value = $('select#dropDownId option:selected').val();

但两者都返回空字符串。


当前回答

使用这些代码中的任何一个

$('#dropDownId :selected').text();

OR

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

其他回答

对于单个select dom元素,获取当前选择的值:

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

获取当前选定的文本:

$('#dropDownId :selected').text();

use

$('#dropDownId').find('option:selected').val()

这应该工作:)

或者你可以试试:

$("#foo").find("select[name=bar]").val();

我今天用它,它工作得很好。

我知道这是一个非常老的帖子,我可能应该为这个可怜的复活而被鞭打,但我想我将分享几个非常有用的小JS片段,我在我的武器库中使用每个应用程序…

如果输入:

$("#selector option:selected").val() // or
$("#selector option:selected").text()

是老了,尝试添加这些小松脆饼到你的全局*.js文件:

function soval(a) {
    return $('option:selected', a).val();
}
function sotext(a) {
    return $('option:selected', a).text();
}

然后写soval("#selector");或sotext(“#选择器”);相反!如果将两者结合并返回一个包含值和文本的对象,那就更好了!

function so(a) {
    my.value = $('option:selected', a).val();
    my.text  = $('option:selected', a).text();
    return my;
}

它为我节省了大量宝贵的时间,尤其是在表单较多的应用程序上!

$("#dropDownId").val('2');
var SelectedValue= $("#dropDownId option:selected").text();
$(".ParentClass .select-value").html(SelectedValue);


<p class="inline-large-label button-height ParentClass" >
     <label for="large-label-2" class="label">Country <span style="color: Red;">*</span><small></small></label>
     <asp:DropDownList runat="server" ID="dropDownId " CssClass="select" Width="200px">          
    </asp:DropDownList>
</p>