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

211
Visualizações
If all checkboxes are checked enable submit button

I have a simple form with some checkboxes and I'm trying to get it so that the submit button is enabled when all the checkboxes are checked.

I've been messing with some of the answers from here: Enable submit button only when all fields are filled

But it seems like it either doesn't enable the submit button or it enables it right after one is checked. Here's what I have:

$(document).on("change keyup", ".required", function(e) {
  let Disabled = true;
  $(".required").each(function() {
    let value = this.value;
    if (value && value.trim() != "") {
      Disabled = false;
    } else {
      Disabled = true;
      return false;
    }
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
  } else {
    $(".toggle-disabled").prop("disabled", false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

It's weird cause it sorta works, but as soon as I check one box the submit button is enabled. Looks like I have all the pieces there, but not sure if it's something within the change keyup or something else.

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

0

  1. You only need an EventListener to check for changes made within the input (checkboxes). You do not need to listen to all changes or keyups on the entire document. So just use $('form input[type="checkbox"]').change(function(){ ... } which going to save resources.
  2. You have to check how many checkboxes have not been checked with $('form input[type="checkbox"]:not(:checked)').length. This will give the exact number of checkboxes that are currently unchecked.
  3. Now you can use an if/else-statement to see if that count is higher than 0 (which means that at least 1 checkbox is still unchecked. In my example I replaced the if/else-statement with a conditional ternary operator to save a few lines:

$('form input[type="checkbox"]').change(function(){
  var allChecked = !!$('form input[type="checkbox"]:not(:checked)').length;
  $('.toggle-disabled').prop('disabled', allChecked);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to keep track of count as suggested by @tacoshy, I have modified your code to reflect the changes.

$(document).on("change keyup", ".required", function(e) {
  let Disabled = $(".required").length;
  $(".required").each(function() {
    if(this.checked) Disabled--;
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
  } else {
    $(".toggle-disabled").prop("disabled", false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

Just compare checked and not checked counts on any change.

  • Give the form an id (event.delegateTarget)
  • count all of the checkboxes (filter for that type)
  • count checked checkboxes
  • compare and set the property based on that
  • scope to the form for the button (might consider filter by input[type="submit"] also
  • trigger on load to set it disabled if not all checked at that point

$("#formid").on("change", ".required", function(event) {
  let allCheckboxes = $(event.delegateTarget)
    .find(".required").filter('input[type="checkbox"]');
  let checkedCount = allCheckboxes.filter(":checked").length;
  $(event.delegateTarget).find(".toggle-disabled")
    .prop("disabled", allCheckboxes.length != checkedCount);
}).trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formid" action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

Alternative

$("#formid").on("change", ".required", function(event) {
  let notAllChecked = !!$(event.delegateTarget)
    .find(".required").filter('input[type="checkbox"]:not(:checked)').length;
   $(event.delegateTarget).find(".toggle-disabled")
    .prop("disabled", notAllChecked);
}).trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formid" action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

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