Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!