Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dantetesta/525b0db3a4ac1a7f6cd815cb7240f203 to your computer and use it in GitHub Desktop.
Save dantetesta/525b0db3a4ac1a7f6cd815cb7240f203 to your computer and use it in GitHub Desktop.
Limitador de Seleção em Checkbox no Backend do WordPress para Taxonomias
<script>
jQuery(document).ready(function($) {
// Função modificada para aceitar limites como parâmetros
function inicializarTratamentoCheckbox(selector, limite) {
$(selector).on('change', function() {
var grupo = $(this).closest('.tabs-panel');
var checkboxesSelecionados = $('input[type="checkbox"]:checked', grupo).length;
if (checkboxesSelecionados > limite) {
$(this).prop('checked', false);
alert('Você só pode selecionar até ' + limite + ' opções neste grupo.');
}
});
}
// Chamadas da função com seletores específicos e seus respectivos limites
inicializarTratamentoCheckbox('#sexochecklist input[type="checkbox"]', 1); // Limite para o grupo 'sexo'
inicializarTratamentoCheckbox('#cursoschecklist input[type="checkbox"]', 3); // Limite para o grupo 'cursos'
//duplique essas linha acima para adicionar novas restrições.
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment