Set value to select2 ajax input

Set value to select2 ajax input

itarodrigoitarodrigo Posts: 15Questions: 6Answers: 0
edited May 2020 in Editor

My code:

{ label: "Município de Residência:", name: "tb_cidadao.ibge", type: "select2", opts: {
          placeholder: "Selecione o município",
          initialValue: true,
          ajax: {
            url: 'dados/localidade.php',
            dataType: 'json',
            delay: 250,
            processResults: function(data){
              return{
                  results: data
              };
            },
            cache: true
          }
        }},

I am trying to set a value to my select2 field using the code below, but it's not works and not generate any error:

editor.field('tb_cidadao.ibge').inst().select2().val('2605004').trigger('change');

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin
    Answer ✓

    I'd suggest using field().val() to set the value - e.g.

    editor.field('tb_cidadao.ibge').val('2605004')
    

    If that doesn't help, can you post a link to a test case showing the issue please?

    Allan

This discussion has been closed.