大家好 ..
是否有人可以帮助我在Js下面。现在它对我很好,但我不能使用退格。
Js下面允许文本,我可以使用删除键,如何允许退格。
$(document).ready(function(){
$("#inputTextBox").keypress(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0 )) {
event.preventDefault();
}
console.log(inputValue);
});
});
在if中使用退格键检查键码8并执行
if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0 ) && !(inputValue ==8)) {
event.preventDefault();
}
供参考https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
$(document).ready(function(){
$("input").keydown(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0) && (inputValue !=8) )
{
event.preventDefault();
}
console.log(inputValue);
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
First name: <input type="text" name="FirstName" ><br>
</body>
</html>
特殊钥匙
资源管理器不会为delete,end,enter,escape,功能键,home,insert,pageUp / Down和tab激活按键事件。
如果你需要检测这些密钥,请自己帮忙并搜索他们的keyCode onkeydown / up,并忽略onkeypress和charCode。