Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Update other fields from data returned in ajax call using select2

I have a working select2 field which updates based on the results of the ajax call, what i now want to do is up two other fields in my template using some of the data which is returned in the ajax call.

The below is the script so far

<script>
    $(document).ready(function () {
        $('#id_site').select2({
            theme: 'bootstrap4',
            placeholder: "Select a site",
            ajax: {
                url: '{% url 'site-view-ajax' %}',
                dataType: 'json',
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {id: item.id, 
                                    text: item.site_name
                                };
                        })
                    };
                }
            },
            minimumInputLength: 3
        });
    });
</script>

What i then want to do is access two additional fields from the returned data and update my template.

<script>
    $(document).ready(function () {
        $('#id_site').select2({
            theme: 'bootstrap4',
            placeholder: "Select a site",
            ajax: {
                url: '{% url 'site-view-ajax' %}',
                dataType: 'json',
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {id: item.id, 
                                    text: item.site_name, 
                                    code: item.site_code, 
                                    address: item.site_address
                                };
                        })
                    };
                }
            },
            minimumInputLength: 3
        });
    });
</script>

and then something like the below

document.getElementById("id_site_code").value = item.site_code;
document.getElementById("id_site_address").value = item.site_address;

Im just not sure how i would go about this as i have very limited javascript knowledge.

Thanks

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda