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

169
Visualizações
How to get selected value in dependent dropdown in jQuery?

I have dependent dropdown and I'm using the get API to read and retrieve the selection option With the current code I'm not able to show the selected value for the selection

I have 2 select function one is State and another one is District. District is dependent to State. Based on the value on the state it will display list of district. With the current code the it's not selecting the current value of the district even thought the district already have value. it just that it did not showed up as selected how do I fix this code ?

This is what I have tried

    $(document).ready(function() {
      $("#state").change(function() {
        let stateID = $(this).val();
        getDist(stateID);
      });
    });

    const getDist = (state) => {
      axios.get(`${url}district?id=${state}`)
        .then(function(res) {
          if (res.data.status == 'success') {
            res.data.result.map((e) => {
              $('#dist').append(`<option value="${e.dist_id}" >${e.dist_name}</option>`);
            });
          }
        });
    }

    function getState() {
      return axios.get(`${url}state`);
    }

    Promise.all([getState(), getDetails(student)])
      .then(function(results) {
        const state = results[0];
        const student = results[1];

        if (state.data.status == 'success') {
          state.data.result.map((e) => {
            $('#state').append(`<option value="${e.st_id}">${e.st_name}</option>`);
          });
        }

        if (student.data.status == 'success') {
          let studData = student.data.result;
          $("#state").val(studData.state_id).change();
          $("#dist").val(studData.district_id).change();
        }
      });


    <div class="col-md-3">
      <div class="form-group">
        <label>State</label>
        <select class="form-control mb-3" id="state">
          <option value="" hidden>Select State</option>
        </select>
      </div>
    </div>

    <div class="col-md-3">
      <div class="form-group">
        <label>District</label>
        <select class="form-control mb-3" id="dist">
          <option value="" hidden>Select District</option>
        </select>
      </div>
    </div>

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