如何使用jQuery获得下拉框的选定值? 我试着用
var value = $('#dropDownId').val();
and
var value = $('select#dropDownId option:selected').val();
但两者都返回空字符串。
如何使用jQuery获得下拉框的选定值? 我试着用
var value = $('#dropDownId').val();
and
var value = $('select#dropDownId option:selected').val();
但两者都返回空字符串。
当前回答
试试这个
$("#yourDropdown option:selected").text();
其他回答
试试这个:
$('#dropDownId option').filter(':selected').text();
$('#dropDownId option').filter(':selected').val();
或者你可以试试:
$("#foo").find("select[name=bar]").val();
我今天用它,它工作得很好。
我知道这是旧的,但我想我更新了一个更最新的答案
$( document ).on( 'change', '#combo', function () {
var prepMin= $("#combo option:selected").val();
alert(prepMin);
});
我希望这对你们有帮助
$("#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值,你只需使用下面的函数just sent id并获得所选值:
function getValue(id){
var value = "";
if($('#'+id)[0]!=undefined && $('#'+id)[0]!=null){
for(var i=0;i<$('#'+id)[0].length;i++){
if($('#'+id)[0][i].selected == true){
if(value != ""){
value = value + ", ";
}
value = value + $('#'+id)[0][i].innerText;
}
}
}
return value;
}