How to get Selected Text from select2 when using <input>

前端 未结 9 1257
心在旅途
心在旅途 2020-12-12 23:38

I am using the select2 control, loading data via ajax. This requires the use of the tag.

Now, I want to retrieve the selecte

9条回答
  •  一个人的身影
    2020-12-12 23:58

    The correct way to do this as of v4 is:

    $('.select2-chosen').select2('data')[0].text

    It is undocumented so could break in the future without warning.

    You will probably want to check if there is a selection first however:

    var s = $('.select2-chosen');
    
    if(s.select2('data') && !!s.select2('data')[0]){
        //do work
    }
    

提交回复
热议问题