Quiero limitar la posibilidad de desmarcar las 3 casillas de verificación.
<input type="checkbox" value="one" checked> one
<input type="checkbox" value="two" checked> two
<input type="checkbox" value="three" checked> three
$(":checkbox").click(function() {
if ($("input:checked").length == 2){
$(":checkbox(:checked)").prop("disabled", false);
}else{
$(":checkbox(:checked)").prop("disabled", true);
}
});
No puedo hacerlo para deshabilitar la última casilla de verificación que queda por no estar marcada. ¡Por favor ayuda!
Respuestas
1 para la respuesta № 1$(":checkbox").click(function() {
if ($("input:checked").length == 1) {
$(":checked").prop("disabled", true);
} else {
$(":checked").prop("disabled", false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="checkbox" value="one" checked>one
<input type="checkbox" value="two" checked>two
<input type="checkbox" value="three" checked>three
0 para la respuesta № 2
Capture el evento de clic y luego impida la acción predeterminada si causara que la cantidad de elementos en los que se hizo clic sea cero.
$("input[type=checkbox]").on("click",function(evt) {
if ($("input[type=checkbox]:checked").length == 0) {
evt.preventDefault();
}
});