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

150
Visualizações
Is there a way to enable/disable button depending on condition of multiple selectbox?

In my Flask app, I have a page with 5 selectbox:

<select onchange="disableBtn()" id="first" name="select_first">
      <option value="option1">Select an option</option>
      {% for each in list %}
      <option value="{{each}}">{{each}}</option>
      {% endfor %}
</select>

I would like to enable/disable the button if only one of the selectbox has an option selected.

function disableBtn() {
    var select = document.getElementById("first");
    var selectedValue = select.options[select.selectedIndex].value;
    if (selectedValue == "option1") {
        document.getElementById("button").disabled = true;
    } else {
        document.getElementById("button").disabled = false;
    }
}

How could I use the same function for all the boxes? I tried with multiple if but it works only for the first.

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

0

You can add a listener for the change event to all select elements, which checks whether an acceptable option was selected for one of the select boxes.

With querySelectorAll you query all select boxes.
With some you iterate over an array and check if a condition is true for at least one element.

const buttonElem = document.getElementById('button');
const selectElems = Array.from(document.querySelectorAll('select[name^="select_"]'));
selectElems.forEach(elem => {
  elem.addEventListener('change', evt => {
    buttonElem.disabled = !selectElems.some(e => e.value !== 'option1');
  });
});

If the button should only be activated if exactly one select box was selected, the code is as follows.

window.addEventListener('DOMContentLoaded', () => {
  const buttonElem = document.getElementById('button');
  const selectElems = Array.from(document.querySelectorAll('select[name^="select_"]'));
  selectElems.forEach(elem => {
    elem.addEventListener('change', evt => {
      buttonElem.disabled = selectElems.filter(e => e.value !== 'option1').length !== 1;
    });
  });
});

All select boxes are filtered for which "option1" was not selected. If the length of the resulting array is not one, the button is disabled.
The code is embedded in a listener that waits until the entire document is loaded.

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