use*_*868 0 javascript ascii keycode
我有一个 HTML 表单,我只需要允许按数字键。为此,我使用了以下代码
$(".span8").keypress(function(e)
{
var unicode=e.charCode?e.charCode : e.keyCode
//警报(Unicode);
if ((unicode ==8) || (unicode==9)|| (unicode==46)){
//如果键不是退格键(我们应该允许)
}
别的
{
if (unicode<48||unicode>57&&unicode!=65) //如果不是数字
return false //禁用按键
}
});
Run Code Online (Sandbox Code Playgroud)
但是在这里,如果我正在测试键码,则删除键的值为 46。46 用于点(-句点)其他值将正确。我无法找到我是不是做错了。请帮忙
我在keypress函数中也发现了这种奇怪的行为。
相反,请尝试以下操作:
jQuery(function($) {
var input = $('.span8');
input.on('keydown', function() {
var key = event.keyCode || event.charCode;
if(key == 8 || key == 46)
//Do something when DEL or Backspace is pressed
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5296 次 |
| 最近记录: |