我正在开发一个旧应用程序以使其与 Firefox 兼容。由于旧应用程序不使用 Jquery,我必须仅使用 Javascript 完成所有工作。
我有一个用于输入日期的输入字段。该字段应该只允许 0-9 的数值。所以我修改了这样的代码以使其兼容firefox。
var event = window.event || ffEvent ; //ffEvent is the function argument
var intKeyCode = event.keyCode || event.which;
if (intKeyCode < 48 || intKeyCode > 57){
if(event.preventDefault){
event.preventDefault();
}
else{
event.returnValue = false;
}
}
但现在的问题是 event.returnValue = false
允许像 Backspace、Tab、Delete、Arrow 按钮
这样的键,而 event.preventDefault()
code> 不允许使用这些按钮。必须允许这些按钮用于输入字段。
那么 Firefox 是否有任何与 event.returnValue=false
行为完全相同的解决方案
最佳答案
为什么不使用 keyCodes 来检查字符是否是数字
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
var event = window.event || ffEvent ; //ffEvent is the function argument
var intKeyCode = event.keyCode || event.which;
if (intKeyCode > 31 && (intKeyCode < 48 || intKeyCode > 57))
return false;
return true;
}
在输入字段的按键中调用此事件,如下
onkeypress="return isNumberKey(event)
关于javascript - event.returnvalue=false 在 Firefox 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27813731/