jQuery Get Selected Option From Dropdown

前端 未结 30 3304
梦如初夏
梦如初夏 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:18

    For good practice you need to use val() to get value of selected options not text().

    <label>Name</label>
    <input type="text" name="name" />
    <select id="aioConceptName">
        <option value="choose">choose io</option>
    </select>
    

    You can use

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

    Or

       $("#aioConceptName :selected").val();
    

    I wish that helps ..

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

    You can select using exact selected option : Below will give innerText

    $("select#aioConceptName > option:selected").text()
    

    While below will give you value.

    $("select#aioConceptName > option:selected").val()
    
    0 讨论(0)
  • 2020-11-22 03:19

    For anyone who found out that best answer don't work.

    Try to use:

      $( "#aioConceptName option:selected" ).attr("value");
    

    Works for me in recent projects so it is worth to look on it.

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

    You can try to debug it this way:

    console.log($('#aioConceptName option:selected').val())
    
    0 讨论(0)
  • 2020-11-22 03:22

    Using jQuery, just add a change event and get selected value or text within that handler.

    If you need selected text, please use this code:

    $("#aioConceptName").change(function () {
        alert($("#aioConceptName :selected").text())
    });
    

    Or if you need selected value, please use this code:

    $("#aioConceptName").change(function () {
        alert($("#aioConceptName :selected").attr('value'))
    });
    
    0 讨论(0)
  • 2020-11-22 03:22

    If you want to grab the 'value' attribute instead of the text node, this will work for you:

    var conceptName = $('#aioConceptName').find(":selected").attr('value');
    
    0 讨论(0)
提交回复
热议问题