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 09:04

This is what works

    var value= $('option:selected', $('#dropDownId')).val();
查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 09:05
$("#selector <b>></b> option:selected").val()

Or

$("#selector <b>></b> option:selected").text()

Above codes worked well for me

查看更多
梦该遗忘
4楼-- · 2018-12-31 09:06

	function fundrp(){
	var text_value = $("#drpboxid option:selected").text();  
	console.log(text_value);
	var val_text = $("#drpboxid option:selected").val();  
	console.log(val_text);
	var value_text = $("#drpboxid option:selected").attr("vlaue") ;
	console.log(value_text);
	var get_att_value = $("#drpboxid option:selected").attr("id") 
	console.log(get_att_value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<select id="drpboxid">
	<option id="1_one" vlaue="one">one</option>
	<option id="2_two" vlaue="two">two</option>
	<option id="3_three" vlaue="three">three</option>              
</select>
<button id="btndrp" onclick="fundrp()">Tracking Report1234</button>

查看更多
何处买醉
5楼-- · 2018-12-31 09:07

To get the jquery value from drop down you just use below function just sent id and get the selected value:

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;
}
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 09:07

If you have more than one dropdown try:

HTML:

<select id="dropdown1" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>
<select id="dropdown2" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>

JavaScript:

    function myFunction(sel) {
        var selected = sel.value;
    }
查看更多
牵手、夕阳
7楼-- · 2018-12-31 09:08

Did you supply your select-element with an id?

<select id='dropDownId'> ...

Your first statement should work!

查看更多
登录 后发表回答