我有以下代码,并且尝试只允许输入 0-9 3 位数字,但仍然允许用户使用箭头键、退格键和删除键。
它在其他浏览器中工作,但在 Firefox 中,控制台的输出显示箭头键和删除键注册为键代码 0。什么给出?
代码:
$input.
keypress(function(e) {
var currentLength = $input.val().length;
var c = e.which;
console.log("key", c);
//0=48, 9=57, 37-40 = arrow keys 8= backspace, 46=delete
if (c === 8 || c === 46 || (c >= 37 && c <=40)) {
//delete, backspace, and arrow keys are allowed
} else if (c < 48 || c > 57 || currentLength >= 3) {
e.preventDefault();
}
}).on("paste", function(e) {
e.preventDefault();
});
fiddle :http://jsfiddle.net/2cjpdfcy/
最佳答案
关于Firefox 中的 JavaScript 键盘键代码 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33615181/