Find name of selected option using jQuery

前端 未结 4 889
囚心锁ツ
囚心锁ツ 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:22

    I think what you are looking for is .filter()

    name: $(this).filter(':selected').text()
    

    It will return empty string if it's not selected

    Good luck!

    Edit:

    I didn't see that Brett had a space before ":selected" which means he is looking for a child. Stefanvds suggestion to use find() will work fine. filter() checks if the current dom is ":selected" while find() is going to look for the children on all levels. You could also use .children() if you know that the selected dom you are looking for is a direct child of "this" as it is a lot more efficient since you are only looking for one level of children.

    name: $(this).children(':selected').text()
    

提交回复
热议问题