jquery keyup在除Firefox之外的所有浏览器中工作

我有这个代码,以防止人们进入文本框’£’

jQuery(document).ready(function(){ jQuery('#cp_price').keypress(function(e){ if(e.keyCode == 163){ alert("Exclude the £ sign"); return false; } }); }); 

它适用于除Firefox以外的所有浏览器。 有什么理由不行吗?

我想你需要。

 jQuery(document).ready(function(){ jQuery('#cp_price').keypress(function(e){ if((e.keyCode ? e.keyCode : e.which) == 163){ alert("Exclude the £ sign"); return false; } }); }); 

使用e.charCodee.which而不是keyCode 。 事件对象的charCode值指的是打印字符。 在Firefox 7.0.1中, e.keyCode等于零。

您可以在http://asquare.net/javascript/tests/KeyCode.html上查看现场演示中的差异

尝试使用

 e.which 

代替

 e.keyCode