这是我的 JS
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]/;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
该脚本适用于 input
标记,只能输入数字。
它在 google chrome 上运行良好,但在 firefox 15.0 上退格、删除、箭头键不起作用
这是firefox的bug,还是我的脚本有问题?
最佳答案
原因是 Chrome 和 IE 不会触发这些特殊键(退格键、删除键、箭头键)的按键事件。
但对于 Firefox,该事件将被触发,并且无法匹配正则表达式,因此将执行 theEvent.preventDefault()
。
关于javascript - JS不支持firefox 15.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12293082/