如何禁用使用jQuery的文本框以外的退格 [英] How to disable backspace except textbox using jQuery

查看:92
本文介绍了如何禁用使用jQuery的文本框以外的退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用下面的代码,但它阻止退格功能包括文本字段。BACKSPACE应该适用于TEXT field only。



请帮助解决...

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

函数processKeyEvents(event){
// Backspace
if(event.keyCode == 9){
// myTextBox是有效文本框的ID
if($(*:focus)!= $(#myTextBox)){
event.preventDefault();




解决方案

检查keydown事件,如果按下 backspace ,则防止默认,如果textarea没有被聚焦。
http://jsfiddle.net/Q9Meh/2/

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


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();
        }
    }
} 

解决方案

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();
   }
});​

这篇关于如何禁用使用jQuery的文本框以外的退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆