jQuery Get Selected Option From Dropdown

前端 未结 30 3306
梦如初夏
梦如初夏 2020-11-22 02:56

Usually I use $(\"#id\").val() to return the value of the selected option, but this time it doesn\'t work. The selected tag has the id aioConceptName

相关标签:
30条回答
  • 2020-11-22 03:23

    Here is the simple solution for this issue.

    $("select#aioConceptName").change(function () {
               var selectedaioConceptName = $('#aioConceptName').find(":selected").val();;
               console.log(selectedaioConceptName);
            });
    
    0 讨论(0)
  • 2020-11-22 03:24

    If you are in event context, in jQuery, you can retrieve the selected option element using :
    $(this).find('option:selected') like this :

    $('dropdown_selector').change(function() {
        //Use $option (with the "$") to see that the variable is a jQuery object
        var $option = $(this).find('option:selected');
        //Added with the EDIT
        var value = $option.val();//to get content of "value" attrib
        var text = $option.text();//to get <option>Text</option> content
    });
    

    Edit

    As mentioned by PossessWithin, My answer just answer to the question : How to select selected "Option".

    Next, to get the option value, use option.val().

    0 讨论(0)
  • 2020-11-22 03:24

    Probably your best bet with this kind of scenario is to use jQuery's change method to find the currently selected value, like so:

    $('#aioConceptName').change(function(){
    
       //get the selected val using jQuery's 'this' method and assign to a var
       var selectedVal = $(this).val();
    
       //perform the rest of your operations using aforementioned var
    
    });
    

    I prefer this method because you can then perform functions for each selected option in a given select field.

    Hope that helps!

    0 讨论(0)
  • 2020-11-22 03:25

    Just this should work:

    var conceptName = $('#aioConceptName').val();
    
    0 讨论(0)
  • 2020-11-22 03:26

    to fetch a select with same class= name you could do this, to check if a select option is selected.

    var bOK = true;
    $('.optKategorien').each(function(index,el){
        if($(el).find(":selected").text() == "") {
            bOK = false;
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:29

    you should use this syntax:

    var value = $('#Id :selected').val();
    

    So try this Code:

    var values = $('#aioConceptName :selected').val();
    

    you can test in Fiddle: http://jsfiddle.net/PJT6r/9/

    see about this answer in this post

    0 讨论(0)
提交回复
热议问题