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

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

and

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

但两者都返回空字符串。


当前回答

或者你可以试试:

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

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

其他回答

use

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

这应该工作:)

试试这个jQuery,

$("#ddlid option:selected").text();

或者这个javascript,

 var selID=document.getElementById("ddlid");
 var text=selID.options[selID.selectedIndex].text;

如果你需要访问值而不是文本,那么尝试使用val()方法而不是text()。

看看下面的小提琴链接。

Demo1 | Demo2

$("#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>

这样就可以了

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

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

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

OR

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