Find name of selected option using jQuery

前端 未结 4 887
囚心锁ツ
囚心锁ツ 2021-02-04 01:21

I\'ve made a jquery/ajax function that updates #courses, sending #fos\'s .val() and .text(), specifically of the one that is selected, like so:

$(\'#selling #fos         


        
4条回答
  •  情歌与酒
    2021-02-04 02:29

    You can also use jQuery's second argument (context) to avoid the unnecessary "filter", "find", "children" etc. This allows your selector to be something like:

    $('select[name="myselect"]').on('change',function(){
        var selectedOptionName = $('option:selected',this).text();
        console.log(selectedOptionName);
    });
    

提交回复
热议问题