Get selected value of a dropdown's item using

2018-12-31 08:08发布

How can I get the selected value of a dropdown box using jQuery?
I tried using

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

and

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

but both return an empty string.

标签: jquery
29条回答
骚的不知所云
2楼-- · 2018-12-31 08:48

Yet another tested example:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $('#bonus').change(function() {
    alert($("#bonus option:selected").text());
  });  
});
</script>
</head>

<body>
<select id="bonus">
<option value="1">-$5000</option>
<option value="2">-$2500</option>
<option value="3">$0</option>
<option value="4">$1</option>
<option value="5">We really appreciate your work</option>
</select>
</body>
</html>
查看更多
情到深处是孤独
3楼-- · 2018-12-31 08:48
$("#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>
查看更多
伤终究还是伤i
4楼-- · 2018-12-31 08:50

I know this is old but I though I update this with an more up to date answer

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

I hope this helps

查看更多
笑指拈花
5楼-- · 2018-12-31 08:53

Try this, it gets the value:

$('select#myField').find('option:selected').val();

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 08:54

Try this:

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

 $('#dropDownId option').filter(':selected').val();
查看更多
时光乱了年华
7楼-- · 2018-12-31 08:56

Or if you would try :

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

I used It today and It working fine.

查看更多
登录 后发表回答