简体   繁体   中英

Select2 selected options with ajax

I'm using Select2 to do some generic filters. I get options from my server with an Ajax request, now I need to set the selected options with data from a Cookie.

I'm trying with:

$("#categoriesselector").select2("val",["01002"]);
$("#categoriesselector").val(["01002"]);
$("#categoriesselector").select2().val(["01002"]);

But this not works. Any idea about what could be happening? Thanks, Iván.

From Select2 docs you have to set data when calling select2, like so:

$('#categoriesselector').select2({
  data: [
    {
      id: '01002',
      text: '01002'
    }
  ]
});

要在select2上显示选定的选项,您必须运行以下行:

$('#id_of_your_select2').val(array_of_selection_ids).trigger('change')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM