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

Try this jQuery,

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

or this javascript,

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

If you need to access the value and not the text then try using val() method instead of text().

Check out the below fiddle links.

Demo1 | Demo2

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 08:57

Use the method below to get the selected value on page load:

$(document).ready(function () {
$('#ddlid').on('change', function () {
                var value = $('#ddlid :selected').text();
                alert(value);`
            });
});
查看更多
妖精总统
4楼-- · 2018-12-31 09:00

For selected text use:

value: $('#dropDownId :selected').text();

For selected value use:

value: $('#dropDownId').val();
查看更多
看淡一切
5楼-- · 2018-12-31 09:00

You need to put like this.

$('[id$=dropDownId] option:selected').val();
查看更多
谁念西风独自凉
6楼-- · 2018-12-31 09:01

this will do the trick

$('#dropDownId').val();
查看更多
倾城一夜雪
7楼-- · 2018-12-31 09:01
$("select[id$=dropDownId]").val()
  • try this
查看更多
登录 后发表回答