How to set selected value of jquery select2?

后端 未结 28 1451
野趣味
野趣味 2020-11-30 00:00

This belong to codes prior to select2 version 4

I have a simple code of select2 that get data from ajax



        
28条回答
  •  没有蜡笔的小新
    2020-11-30 00:41

    To dynamically set the "selected" value of a Select2 component:

    $('#inputID').select2('data', {id: 100, a_key: 'Lorem Ipsum'});
    

    Where the second parameter is an object with expected values.

    UPDATE:

    This does work, just wanted to note that in the new select2, "a_key" is "text" in a standard select2 object. so: {id: 100, text: 'Lorem Ipsum'}

    Example:

    $('#all_contacts').select2('data', {id: '123', text: 'res_data.primary_email'});
    

    Thanks to @NoobishPro

提交回复
热议问题