Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda