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

126
Visualizações
.val() returns null in jquery

I have the following code:

let selectedSub = $('#substation-select').val();
if (selectedSub == undefined) {
  selectedSub = $('#substation-select option:first()').val();
}

loadAvailableAmbulances(selectedSub);

let selectedAmb = $('#ambulance-select').val();
if (selectedAmb == undefined) {
  selectedAmb = $('#ambulance-select option:first()').val();
}

function loadAvailableAmbulances(selectedSub) {
  $.ajax({
    type: "GET",
    data: {
        substation: selectedSub
    },
    url: "/available-ambulances",
    success: function(response) {
      $('#ambulance-select').append(response);
    }
});
}

What this piece of code does is actually generating the options for a select2. As you can see, I have an ajax request that triggers this function:

public function available_ambulances(Request $request) 
    {
        $ambulances = Ambulance::whereHas('checklist', function($query) use($request) {
            $query->where('used', 0);
            $query->where('inventory_id', $request->substation);
        })
        ->get();

        $html = "";

        foreach($ambulances as $ambulance) {
            $html .= sprintf(
                '<option value="%s">%s</option>',
                $ambulance->id,
                $ambulance->license_plate
            );
    }

        return response($html, 200);
    }

For some reason, in my jquery code, selectedAmb is null, even though I call the function loadAvailableAmbulances(selectedSub). It should get me the value of the first ambulance that is generated. Why is it returning null?

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