Adding “data-” attributes with select2

后端 未结 3 1349
眼角桃花
眼角桃花 2020-12-15 06:11

I\'ve seen a lot examples of Select2 option tags set with \"data-\" attributes and I would like to do it.

I\'m using ajax to get the data. I get the

3条回答
  •  死守一世寂寞
    2020-12-15 07:09

    This solution applies to Select2 versions 4.0 or higher.

    Assuming the attributes your talking about are loaded in the array you are returning in processResults. For example, if you are selecting a record like ('id':1,'text':'some-text','custom_attribute':'hello world')

    Then on a change event you can do:

    data=$("#selector").select2('data')[0];
    console.log(data.custom_attribute);//displays hello world
    

    Hope it helps..

提交回复
热议问题