我在輸入中遇到了避免特殊字符(Alt +數字來自Bloq。num - >☺☻♥♦♣♠•◘○)的問題。當Alt鍵加上來自Bloq號碼的號碼被按下時,阻止按鍵
我輸入聲明如下:
<html:text styleId="inputMaxVol" styleClass="numeric"/>
這是我的onReady
$(".numeric").keydown(function(event) {
return maskNumber();
});
而這裏就是我想要做的工作(沒有成功)
function maskNumber() {
if (event) {
var charCode = (event.which)? event.which : event.keyCode;
console.log(charCode);
// If not a number
if ((charCode < 47 || charCode > 58) && (charCode < 95 || charCode > 105)) {
console.log("not a number");
return false;
} else if (event.altKey) {
//No special characters admitted
console.log("Alt pressed");
return false;
}
} else {
return false;
}
}
如果我按下Alt + 1(數字鍵盤上的鍵「1」),出現白色笑臉,並在我的日誌中出現一些東西我認爲
18
not a number
97
Alt pressed
我也試過了event.preventDefault()沒有成功。任何關於我的問題在哪裏的想法?
謝謝,我真的很感謝你能提供任何幫助。
UPDATE:作爲註釋解釋瞭解決方案來改變對按鍵的keydown事件和變化中charCode比較 - 數字(現在是僅當(charCode < 48 || charCode > 57
)由於返回的數量也發生了變化
所以。解決方法如下
$(".numeric").keypress(function(event) {
return maskNumber();
});
function maskNumber() {
if (event) {
var charCode = (event.which)? event.which : event.keyCode;
// If not a number
if (charCode < 48 || charCode > 57) {
return false;
}
} else {
return false;
}
}
就這麼簡單!
試試這個庫:https://開頭github.com/tzuryby/jquery.hotkeys – brijrajsinh