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

262
Visualizações
Change the value of a select2 when I modify the value in another select2

I have two select2. The first one fills in automatically and selects the first default value.

My second select2 that shows data depending on the data that the first select2 has, works by AJAX.

My problem is that I want when the value of the first select2 changes, the second select2 is updated and selects the first value from the list of options.

I have seen that in other posts and it has not worked for me. Investigating more on my own I have realized what my problem is.

The second select2 doesn't update the values, fetching them from the database until I click on it.

I would like this second select2 that works by ajax to search for the values ​​automatically when I change the option of the first select2 and to select the first resulting option for me.

I attached the code so you can get an idea

    function filtersTable() {
    let center_select = $('#center_bookings_today_table');

    $.each(centers, function (i, center) {
        center_select.append('<option value="' + i + '" title="' + center + '">' + center + '</option>');
    });
    
    $('#center_bookings_today_table').select2({
        placeholder: '',
    });

    getGroupsTable();
}

function getGroupsTable() {
    let center = $('#center_bookings_today_table').val();
    $("#group_bookings_today_table").val('').trigger('change');
    let data = '?center=' + center;

    $( "#terminals_hours" ).empty();
    $('#restrictions').hide();

    $("#group_bookings_today_table").select2({
        ajax: {
            url: '{{ route('get_search_groups_center', ['client' => $client]) }}' + data,
            dataType: 'json',
            processResults: function (response) {
                return {
                    results: response
                };
            },
            cache: true
        },
        language: {
            inputTooShort: function () {
                return '{{ trans('common.insert_chars') }}';
            }
        }
    });
    $("#chart_bookings_today_table").hide();
   }

Thanks in advance. Excuse my English I'm using google translator

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