Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
¿Cómo puedo seleccionar una cantidad aleatoria de casillas de verificación?
 $('#select_all').on('click', function () { if (this.checked) { $('.check').each(function () { this.checked = true; }); } else { $('.check').each(function () { this.checked = false; }); } }); $('.check').on('click', function () { if ($('.check:checked').length == $('.check').length) { $('#select_all').prop('checked', true); } else { $('#select_all').prop('checked', false); } });

Debe modificar este código Seleccionar todo anterior para seleccionar solo una cantidad aleatoria de casillas de verificación, como el ejemplo de código a continuación. ¿Alguna ayuda para esto?

 $(".random-pick").click(function() { var maxAllowed = 6; // Count checkboxes var random_checkboxes = $("#content_subscribtion_ input.checkbox").size(); // Check random checkboxes until "maxAllowed" limit reached while ($("#content_subscribtion_ input.checkbox:checked").size() < maxAllowed) { // Pick random checkbox var random_checkbox = Math.floor(Math.random() * random_checkboxes) + 1; // Check it $("#content_subscribtion_ input.checkbox:nth-child(" + random_checkbox + ")").prop("checked", true); } return false; });
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Con este pequeño y ordenado método de reproducción aleatoria , podemos crear una matriz aleatoria de índices de casillas de verificación y luego dividirla en un número aleatorio. Luego podemos iterar y usar get() de jQuery para encontrar el índice de la casilla de verificación en la matriz aleatoria para verificar.

 Array.prototype.shuffle = function() { let m = this.length, i; while (m) { i = (Math.random() * m--) >>> 0; [this[m], this[i]] = [this[i], this[m]] } return this; } $('#select_all').on('click', function() { if ($(this).prop('checked')) { let minnum = 3, maxnum = 6 let rand = Math.min(maxnum, Math.floor(Math.random() * ($('.check').length - 1 - minnum)) + minnum) //create our keys array let keyArray = [...Array($('.check').length).keys()].shuffle().slice(0, rand) keyArray.forEach((chk_i, i) => { if (i < rand) $($('.check').get(chk_i)).prop('checked', true) }) } else { $('.check').prop('checked', false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>Select all <input type='checkbox' id="select_all"></label> <div class='cb'> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> <input type='checkbox' class='check'> checkbox <br /> </div>

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda