如果问题可能有点宽泛,我很抱歉,但我已经尝试了所有方法,但我无法找到替代它的东西(这不是我的,我只是在使用它):
function onlyNum(evt){
evt = window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
}
return true;
}
// this INPUT will not accept anything but numbers now.
<input type="text" id="idInput" onKeyPress="return onlyNum(event)">
有了这个:
function onlyNum(evt){
evt = window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
}
return true;
}
// this INPUT's with this event listeners will just acept numbers now.
// like this code is cleaner since you just need to make a list of ids that will have this listeners.
var input = document.getElementById('idTest');
input.addEventListener('keyDown', onlyNum, false);
我找不到将事件传递给函数和“返回”部分的方法。抱歉,我什至不知道如何描述它,也许这就是为什么我找不到有关此问题的任何信息的原因...