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

259
Vistas
select 2 ajax success only works once

I am using the select2 value to populate other read-only fields in the same form, my question is why the ajax success function populates the field only once but when I do something(solution) wrong it is called the success function multiple times?

<script>
  $(document).ready(function () {
    $('#barang_id').select2({
      placeholder: 'Cari...',
      allowClear: true,

      ajax: {
        url: '/api/barang',
        contentType: 'application/json; charset=utf-8',
        data: function (params) {
          var query = {
            term: params.term,
          };
          return query;
        },
        headers: {
          RequestVerificationToken: $(
            'input[name="__RequestVerificationToken"]'
          ).val(),
        },
        processResults: function (data) {
          return {
            results: $.map(data, function (item) {
              return {
                id: item.id,
                text: item.kode + ' - ' + item.nama,
              };
            }),
          };
        },
        success: function (data) {
          $.map(data, function (item) {
            $('#nama').val(item.nama);
            $('#kode').val(item.kode);
          });
        },
      },
    });
  });
</script>
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