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:45

try this

$("#yourDropdown option:selected").text();
查看更多
查无此人
3楼-- · 2018-12-31 08:45

You can use any of these:

$(document).on('change', 'select#dropDownId', function(){

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

            //OR

            var value = $(this).val();

});
查看更多
姐姐魅力值爆表
4楼-- · 2018-12-31 08:46

This will alert the selected value. JQuery Code...

$(document).ready(function () {

        $("#myDropDown").change(function (event) {
            alert("You have Selected  :: "+$(this).val());
        });
    });

HTML Code...

<select id="myDropDown">
        <option>Milk</option>
        <option>Egg</option>
        <option>Bread</option>
        <option>Fruits</option>
    </select>
查看更多
后来的你喜欢了谁
5楼-- · 2018-12-31 08:47
var value = $('#dropDownId:selected').text()

Should work fine, see this example:

$(document).ready(function(){ 
  $('#button1').click(function(){ 
    alert($('#combo :selected').text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="combo">
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
</select>
<input id="button1" type="button" value="Click!" />

查看更多
看淡一切
6楼-- · 2018-12-31 08:47

this should work for you

  $("#dropDownId").on('change',function(){
     var value=$(this).val();
     alert(value);
  });
查看更多
余欢
7楼-- · 2018-12-31 08:48

For single select dom elements, to get the currently selected value:

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

To get the currently selected text:

$('#dropDownId :selected').text();
查看更多
登录 后发表回答