Enable input when arriving at selected checkbox amount

Asked

Viewed 186 times

0

Guys I got this input:

<input type="submit" id="cadastrar" value="Cadastrar" disabled>

I have a grid that is filled and each line has one checkbox:

<td>
    <input type="checkbox" id="seleciona" name="seleciona[]" value= <=$row_questao['codprova'] ?>" />
</td>

I have this script that when I click only on the first checkbox from Grid it enables and disables, from 2nd onwards it does not work:

$(document).ready(function () {
  $("#seleciona").on('change', function () {
    if ($(this).is(':checked')) {
      $("#cadastrar").prop('disabled', false);
    } else {
      $("#cadastrar").prop('disabled', true);
    }
  });
});

I need him to stay enabled when a quantity x will come from a mysql query checkbox is selected and when you deselect 1 of the total it returns to disabled.

  • hi, try to supplement your code as much as possible of the result, with the rest of html to perform a test, if possible make your code available inside the snippet in the question editor.

  • hello, the rest to complement would be html unimportant.

1 answer

2


You are repeating the id="seleciona", and with this Javascript will always find the first one it finds. So change only works in the first checkbox.

An id must be unique on the page, it cannot be repeated.

If you want to enable input only when a given minimum number is marked, just compare that number with the quantity that has been marked. If the quantity marked is equal to or greater than the minimum, enables the input, if it is smaller, disable.

The minimum number to be marked you can put in an attribute data-* in the input #register:

<input type="submit" data-min="2" id="cadastrar" value="Cadastrar" disabled>
                         ↑

To do this you don’t even need id’s, simply select the elements by name, since everyone has the same:

$(function(){

   $(":checkbox[name='seleciona[]']").on("change", function(){
      // pega o valor mínimo
      var minimo = $("#cadastrar").data("min");
      // conta quantos foram marcados
      var marcados = $(":checkbox[name='seleciona[]']:checked").length;
      
      $("#cadastrar").prop("disabled", marcados >= minimo ? false : true );
      
   });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="submit" data-min="2" id="cadastrar" value="Cadastrar" disabled>
<br>
Marque pelo menos 2 checkbox para habilitar o campo acima:
<br>
<input type="checkbox" name="seleciona[]" value="a" />
<input type="checkbox" name="seleciona[]" value="b" />
<input type="checkbox" name="seleciona[]" value="c" />

  • Perfect @Sam, it was exactly what I wanted, but another question arose as it would lead the total checks to be marked coming from a select in mysql?

  • What do you mean? I don’t understand.

  • The total check’s to be marked will not be the total that is on the screen, in your ex: 3, will be some 30 to 50 checks on the grid, hence I will have a select that will return a value and this value is quantity to be checked to enable the input. instead of being var total = $(":checkbox[name='seleciona[]']").length;, it would have to be something like this: var total = $variavel['quatidade'];

  • I changed the answer. See if that’s it.

  • That’s right, ball show, vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.