Programmatic selection of select2 which retrieves its data via Ajax

后端 未结 4 1698
野趣味
野趣味 2021-02-19 10:16

I am using select2 version 4.0, and I am trying to make a programmatic selection to a select box which gets its data from an ajax call.

In the documentation, I found how

4条回答
  •  伪装坚强ぢ
    2021-02-19 10:55

    The only way to achieve this, is to first add an to the select dom with the data you would like to select, and right afterwards, select it like you would do with a regular select box. This was the official answer from kevin-brown also, on their github project page.

    https://github.com/select2/select2/issues/3273

    So, basicaly, if you know what would you like to select, you can easily create an element from it, add it to the original select box, and then select it by it's value:

    var option=jQuery("

提交回复
热议问题