Get Select, not val



  • Hello, everyone.

    <select name="s">
            <option value="1">Москва</option>
            <option value="2">Санкт-Петербург</option>
        </select>
    

    if obtained val()I get through the id, and how do you get the city name? Importantly, id in value needs to be left to record in OBD.

    UPD get the assigned value, for example, if Moscow chose to return Moscow, not 1



  • $('select').on('change', function() {
        console.log($("select option:selected").text());
    
    // или чуть правильнее:
    
    console.log($(this).find(":selected").text());
    

    });

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
    <select name="s">
    <option value="1">Москва</option>
    <option value="2">Санкт-Петербург</option>
    </select>


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2