How to get $(this) selected option in jQuery?

后端 未结 9 1209
青春惊慌失措
青春惊慌失措 2020-12-13 01:18

The following code works:

$(\"#select-id\").change(function(){
  var cur_value = $(\'#select-id option:selected\').text();
  . . .
});

How

相关标签:
9条回答
  • 2020-12-13 01:45
    var cur_value = $('option:selected',this).text();
    
    0 讨论(0)
  • 2020-12-13 01:47

    Best guess:

    var cur_value = $('#select-id').children('option:selected').text();
    

    I like children better in this case because you know you're only going one branch down the DOM tree...

    0 讨论(0)
  • 2020-12-13 01:47

    It's just

    $(this).val();
    

    I think jQuery is clever enough to know what you need

    0 讨论(0)
  • 2020-12-13 01:51

    For the selected value: $(this).val()

    If you need the selected option element, $("option:selected", this)

    0 讨论(0)
  • 2020-12-13 01:53

    Best and shortest way in my opinion for onchange events on the dropdown to get the selected option:

    $('option:selected',this);
    

    to get the value attribute:

    $('option:selected',this).attr('value');
    

    to get the shown part between the tags:

    $('option:selected',this).text();
    

    In your sample:

    $("#select-id").change(function(){
      var cur_value = $('option:selected',this).text();
    });
    
    0 讨论(0)
  • 2020-12-13 01:57
     $(this).find('option:selected').text();
    
    0 讨论(0)
提交回复
热议问题