Last active
April 3, 2024 10:44
-
-
Save dantetesta/525b0db3a4ac1a7f6cd815cb7240f203 to your computer and use it in GitHub Desktop.
Limitador de Seleção em Checkbox no Backend do WordPress para Taxonomias
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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