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

114
Visualizações
Ajax data Option if no button disabled

I needed help with my button to be disabled that if the option selected if "No" it be disabled and if "Yes" it be enabled. I'm using my AJAX to call for the option.

This is my HTML code for the <option> and <button>:

<label for="select-Require-CDS" class="col-form-label">Require CDS</label> <br/>

<select class="select-Require-CDS custom-select-sm col-6" id="select-Require-CDS" required>
   <option selected disabled value="">Select ....</option>
</select>

<div class="invalid-feedback">
   Please select a valid option .
</div>

<button id="select-Yes-No" style="margin-left: 10px; ">Select</button>

This is my AJAX code:

$.ajax({
    // The url that you're going to post. This is the url that you're going to put to call the backend api,
    // in this case, it's  https://ecoexchange.dscloud.me:8080/api/get (production env)
    url: "https://ecoexchange.dscloud.me:8090/api/get",
    
    // The HTTP method that you're planning to use i.e
    // GET, POST, PUT, DELETE. In this case it's a get method, so we'll use GET
    method: "GET",
    
    // In this case, we are going to use headers as 
    headers: {
        // The query you're planning to call; i.e. <query> can be UserGet(0), RecyclableGet(0), etc.
        query: "RequireGet()",
  
       // Gets the apikey from the sessionStorage
        apikey: sessionStorage.getItem("apikey")
    },
    success: function(data, textStatus, xhr) {
        console.log(data);
      
        for (let option of data)
        {
            $('#select-Require-CDS').append($('<option>', {
                value: option.RequireOption,
                text: option.RequireOption
            }));
        }
    },
    error: function(xhr, textStatus, err) {
        console.log(err);
    }
});

And this is how my JSON response is:

[
    {
        "RequireOption": "No"
    },
    {
        "RequireOption": "Yes"
    }
]

So my website display it be like this

enter image description here

Now how can I say that I select option as "No" the button is disabled but if "Yes" is enabled?

enter image description here

How that if no it be disabled as well ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$('#select-Require-CDS').on('change', function(){
    if($(this).val()=='Yes'){
        $('#select-Yes-No').attr('disabled',false) //button is now enabled
    }else{
        $('#select-Yes-No').attr('disabled',true) //button is now disabled
    }
});

This is an handler listening for change on the select. Based on what is selected then it adapt the button availability. It is untested but should do the trick.

I have used .on because you are adding the attributes later with ajax (later = after dom ready) so .change will not work on these options

about 4 years ago · Juan Pablo Isaza Relatório
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