Hay una jQuery plugin called capslockstate que supervisará el estado de la tecla de bloqueo de mayúsculas en toda la página, no solo en campos específicos.
Puede consultar el estado de la tecla de bloqueo de mayúsculas o definir los detectores de eventos para reaccionar a los cambios de estado. El plugin hace un mejor trabajo de detección y administración estatal que otras sugerencias aquí, incluyendo trabajar con teclados no ingleses, monitorear el uso de la tecla Bloq Mayús y no olvidar el estado si no se escriben caracteres alfabéticos .
Hay dos demostraciones, one showing basic event binding y otra showing the warning only when the password field has focus.
p. Ej.
$(document).ready(function() {
/*
* Bind to capslockstate events and update display based on state
*/
$(window).bind("capsOn", function(event) {
$("#statetext").html("on");
});
$(window).bind("capsOff", function(event) {
$("#statetext").html("off");
});
$(window).bind("capsUnknown", function(event) {
$("#statetext").html("unknown");
});
/*
* Additional event notifying there has been a change, but not the state
*/
$(window).bind("capsChanged", function(event) {
$("#changetext").html("changed").show().fadeOut();
});
/*
* Initialize the capslockstate plugin.
* Monitoring is happening at the window level.
*/
$(window).capslockstate();
// Call the "state" method to retreive the state at page load
var initialState = $(window).capslockstate("state");
$("#statetext").html(initialState);
});
y
$(document).ready(function() {
/*
* Bind to capslockstate events and update display based on state
*/
$(window).bind("capsOn", function(event) {
if ($("#Passwd:focus").length > 0) {
$("#capsWarning").show();
}
});
$(window).bind("capsOff capsUnknown", function(event) {
$("#capsWarning").hide();
});
$("#Passwd").bind("focusout", function(event) {
$("#capsWarning").hide();
});
$("#Passwd").bind("focusin", function(event) {
if ($(window).capslockstate("state") === true) {
$("#capsWarning").show();
}
});
/*
* Initialize the capslockstate plugin.
* Monitoring is happening at the window level.
*/
$(window).capslockstate();
});
The code for the plugin es visible en GitHub.
@twodayslate +1 valiosa respuesta también ... Pero estaba buscando jquery –
+1, pero ¿por qué hacer variables globales 'kc' y' sk'? Debes declararlos dentro de la función usando 'var'. También '(kc == 16)' ya es una expresión bool, no es necesario envolverlo en '((kc == 16)? True: falso)' – Keith
+1 muy agradable y útil para mí gracias .. . :) – abhi