Get selected value of a dropdown's item using jQuery

后端 未结 30 1570
广开言路
广开言路 2020-11-22 06:48

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

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

and



        
相关标签:
30条回答
  • 2020-11-22 07: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>

    0 讨论(0)
  • 2020-11-22 07:07

    I know this is a terribly old post and I should probably be flogged for this pitiful resurrection, but I thought I would share a couple of VERY helpful little JS snippets that I use throughout every application in my arsenal...

    If typing out:

    $("#selector option:selected").val() // or
    $("#selector option:selected").text()
    

    is getting old, try adding these little crumpets to your global *.js file:

    function soval(a) {
        return $('option:selected', a).val();
    }
    function sotext(a) {
        return $('option:selected', a).text();
    }
    

    and just write soval("#selector"); or sotext("#selector"); instead! Get even fancier by combining the two and returning an object containing both the value and the text!

    function so(a) {
        my.value = $('option:selected', a).val();
        my.text  = $('option:selected', a).text();
        return my;
    }
    

    It saves me a ton of precious time, especially on form-heavy applications!

    0 讨论(0)
  • 2020-11-22 07:07

    Did you supply your select-element with an id?

    <select id='dropDownId'> ...
    

    Your first statement should work!

    0 讨论(0)
  • 2020-11-22 07:10

    Or if you would try :

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

    I used It today and It working fine.

    0 讨论(0)
  • 2020-11-22 07:11

    use either of these codes

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

    OR

    $('#dropDownId').text();
    
    0 讨论(0)
  • 2020-11-22 07:13

    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;
    }
    
    0 讨论(0)
提交回复
热议问题