如何在键下事件处理程序中停止keypress
事件?
如何在键下事件处理程序中停止keypress
事件?
即使从keydown开始,你也无法停止keypress...它们是两个非常独立的事件。您可以在读取字符后取消keydown/keypress。以下是我为了只允许字母和数字输入文本框(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;
});