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

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

and

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

但两者都返回空字符串。


当前回答

您可以通过使用以下代码来实现这一点。

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

如果您想从选择列表的选项中获取所选值。这样就可以了。

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

其他回答

$("#selector <b>></b> option:selected").val()

Or

$("#selector <b>></b> option:selected").text()

以上代码对我来说工作得很好

如果你有一个以上的下拉尝试:

HTML:

<select id="dropdown1" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>
<select id="dropdown2" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>

JavaScript:

    function myFunction(sel) {
        var selected = sel.value;
    }

试试这个jQuery,

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

或者这个javascript,

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

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

看看下面的小提琴链接。

Demo1 | Demo2

试试这个:

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

 $('#dropDownId option').filter(':selected').val();
$("#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>