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

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

and

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

但两者都返回空字符串。


当前回答

这是可行的

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

其他回答

您是否为选择元素提供了id?

<select id='dropDownId'> ...

你的第一句话应该有用!

我知道这是一个非常老的帖子,我可能应该为这个可怜的复活而被鞭打,但我想我将分享几个非常有用的小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>
var value = $('#dropDownId:selected').text()

应该工作得很好,请看这个例子:

$(文档)时函数(){ $ (' # button1 ') .click(函数(){ 警报($ #组合:选择)。text ()); }); }); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本> <选择id = "组合" > <option value="1">Test 1</option> . </option> . <option value="2">Test 2</option> . </option> . < /选择> <input id="button1" type="button" value="点击!"/>

我知道这是旧的,但我想我更新了一个更最新的答案

$( document ).on( 'change', '#combo', function () {
var prepMin= $("#combo option:selected").val();
 alert(prepMin);
});

我希望这对你们有帮助