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

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

The following code works:

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

How

9条回答
  •  猫巷女王i
    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();
    });
    

提交回复
热议问题