如何在keydown中停止按键事件 [英] how to stop keypress event in keydown

查看:434
本文介绍了如何在keydown中停止按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在keydown处理程序中停止按键事件。

how can i stop keypress event in keydown handler.

推荐答案

即使从keydown也无法停止按键操作。他们都是非常独立的事件。你可以做的是在阅读角色后取消keydown / keypress。这是我做的只允许字母和数字输入文本框(jQuery)

You can't stop the keypress even from keydown... they're both very separate events. What you can do is cancel the keydown / keypress after reading the character. Here's what I do to allow only letters and numbers to by typed into a text box (jQuery)

  urlBox.keypress(function(e){
            if(e.which == 13 || e.which == 8 || e.which == 0) return true;
            if(48 <= e.which && e.which <= 57) return true;
            if(65 <= e.which && e.which <= 90) return true;
            if(97 <= e.which && e.which <= 122) return true;
            return false;                 
          });

这篇关于如何在keydown中停止按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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