Cómo deshabilitar el retroceso excepto el cuadro de texto usando jQuery

I want to disable BACKSPACE button except when its in TEXT field.

I am using following code but it prevent backspace functionality include text field.. BACKSPACE should work for TEXT field only..

Please help this out...

$(document).on("keydown", processKeyEvents);
$(document).on("keypress", processKeyEvents);

function processKeyEvents(event) {
    // Backspace
    if (event.keyCode == 9) {
        // myTextBox is id of the valid textbox
        if ($("*:focus") != $("#myTextBox")) {
            event.preventDefault();
        }
    }
} 

preguntado el 23 de diciembre de 12 a las 15:12

I imported the content of your duplicate post: stackoverflow.com/questions/14017224/… -

2 Respuestas

Check keydown events and if backspace is pressed preventdefault if a textarea is not focused. http://jsfiddle.net/Q9Meh/2/

$('body').keydown(function(event) {
    if (event.which == 8 && !$('textarea:focus').length) {
     event.preventDefault();
   }
});​

Respondido el 23 de diciembre de 12 a las 15:12

Hi Friends, Thanks for your responses but I didnt met my requirement. - PAM

Hi Friends, Thanks for your responses but It didnt met my requirementas of now. Actually I want to disable Backspace button(keyboard button) always. but when text getting focus it should be enable. - PAM

¿Qué tal esto? myTextBox is the id of your textbox in which backspace should work.

$(document).on("keydown", processKeyEvents);
$(document).on("keypress", processKeyEvents);

function processKeyEvents(event) {
    // Backspace
    if (event.keyCode == 8) {
        // myTextBox is id of the valid textbox
        if ($("*:focus") != $("#myTextBox")) {
            event.preventDefault();
        }
    }
}

Respondido el 24 de diciembre de 12 a las 04:12

Hi Friends, Thanks for your responses but It didnt met my requirementas of now. Actually I want to disable Backspace buttnon(keyboard button) always. but when text get focus should be enable. - PAM

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.