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

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

and

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

但两者都返回空字符串。


当前回答

对于正常页面加载下拉列表

  $("#dropDownId").on('change',function(){
     var value=$(this).val();
     alert(value);
  });

确保保持id的唯一性

  $(document).on('change','#dropDownId',function(){
    var value=$(this).val();
    alert(value)
  });

其他回答

或者你可以试试:

$("#foo").find("select[name=bar]").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>

试试这个jQuery,

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

或者这个javascript,

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

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

看看下面的小提琴链接。

Demo1 | Demo2

use

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

这应该工作:)

HTML:

<select class="form-control" id="SecondSelect">
       <option>5<option>
       <option>10<option>
       <option>20<option>
       <option>30<option>
</select>

JavaScript:

var value = $('#SecondSelect')[0].value;