Get the selected option id with jQuery

前端 未结 4 915
孤独总比滥情好
孤独总比滥情好 2020-12-02 10:31

I\'m trying to use jQuery to make an ajax request based on a selected option.

Is there a simple way to retrieve the selected option id (e.g. \"id2\"

相关标签:
4条回答
  • 2020-12-02 10:44

    Th easiest way to this is var id = $(this).val(); from inside an event like on change.

    0 讨论(0)
  • 2020-12-02 10:54

    var id = $(this).find('option:selected').attr('id');

    then you do whatever you want with selectedIndex

    I've reedited my answer ... since selectedIndex isn't a good variable to give example...

    0 讨论(0)
  • 2020-12-02 11:03
    $('#my_select option:selected').attr('id');
    
    0 讨论(0)
  • 2020-12-02 11:05

    You can get it using the :selected selector, like this:

    $("#my_select").change(function() {
      var id = $(this).children(":selected").attr("id");
    });
    
    0 讨论(0)
提交回复
热议问题