jQuery Get Selected Option From Dropdown

前端 未结 30 3377
梦如初夏
梦如初夏 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:29
    $('nameofDropDownList').prop('selectedIndex', whateverNumberasInt);
    

    Imagine the DDL as an array with indexes, you are selecting one index. Choose the one which you want to set it to with your JS.

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

    For get value of tag selected:

     $('#id_Of_Parent_Selected_Tag').find(":selected").val();
    

    And if you want to get text use this code:

     $('#id_Of_Parent_Selected_Tag').find(":selected").text();
    

    For Example:

    <div id="i_am_parent_of_select_tag">
    <select>
            <option value="1">CR7</option>
            <option value="2">MESSI</option>
    </select>
    </div>
    
    
    <script>
     $('#i_am_parent_of_select_tag').find(":selected").val();//OUTPUT:1 OR 2
     $('#i_am_parent_of_select_tag').find(":selected").text();//OUTPUT:CR7 OR MESSI
    </script>
    
    0 讨论(0)
  • 2020-11-22 03:31

    I hope this also helps to understand better and helps try this below,

    $('select[id="aioConceptName[]"] option:selected').each(function(key,value){
       options2[$(this).val()] = $(this).text();
       console.log(JSON.stringify(options2));
    });
    

    to more details please http://www.drtuts.com/get-value-multi-select-dropdown-without-value-attribute-using-jquery/

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

    try to this one

    $(document).ready(function() {
    
        $("#name option").filter(function() {
            return $(this).val() == $("#firstname").val();
        }).attr('selected', true);
    
        $("#name").live("change", function() {
    
            $("#firstname").val($(this).find("option:selected").attr("value"));
        });
    });
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <select id="name" name="name"> 
    <option value="">Please select...</option> 
    <option value="Elvis">Elvis</option> 
    <option value="Frank">Frank</option> 
    <option value="Jim">Jim</option> 
    </select>
    
    <input type="text" id="firstname" name="firstname" value="Elvis" readonly="readonly">
    
    0 讨论(0)
  • 2020-11-22 03:32

    You can use $("#drpList").val();

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

    Try

    aioConceptName.selectedOptions[0].value
    

    let val = aioConceptName.selectedOptions[0].value
    
    console.log('selected value:',val);
    <label>Name</label>
    <input type="text" name="name" />
    <select id="aioConceptName">
        <option>choose io</option>
        <option>roma</option>
        <option>totti</option>
    </select>

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