Javascript Textfield不允许我在Firefox的按键上输入字符

Javascript Textfield不允许我在Firefox的按键上输入字符,javascript,html,firefox,Javascript,Html,Firefox,我有一个表单,当我通过按键输入所有其他浏览器的字符时,它允许用户这样做。但当我使用Firefox时,它不允许用户通过按键输入字符,而是允许用户复制和粘贴。下面是我的脚本: <script language="javascript"> function aaa(){ document.getElementById('txtwebref').style.color = 'black'; } </script> <input name="txtw

我有一个表单,当我通过按键输入所有其他浏览器的字符时,它允许用户这样做。但当我使用Firefox时,它不允许用户通过按键输入字符,而是允许用户复制和粘贴。下面是我的脚本:

<script language="javascript">
function aaa(){
            document.getElementById('txtwebref').style.color = 'black';
}
</script>

<input name="txtwebref" type="text" id="txtwebref" size="50" value="Insert reference number" style="width:350px; height:50px; border:1px solid #000; border-radius:15px; text-align:center;color:#CCC; font-family:Arial, Helvetica, sans-serif; font-size:24px;" onkeypress="if ( isNaN(this.value + String.fromCharCode(event.keyCode) )) return false;" onfocus="blank(this);" onblur="unblank(this)" onclick="aaa();"/>

函数aaa(){
document.getElementById('txtwebref').style.color='black';
}
您需要

onkeypress="return !isNaN(this.value + String.fromCharCode(event.charCode))"

window.event
不是标准的JavaScript

也可以使用“DOM方式”,为
onkeypress
属性分配一个函数。它的第一个参数将是一个事件对象

像这样:

<input name="txtwebref" type="text" id="txtwebref" size="50" value="Insert reference number" style="width:350px; height:50px; border:1px solid #000; border-radius:15px; text-align:center;color:#CCC; font-family:Arial, Helvetica, sans-serif; font-size:24px;" onfocus="blank(this);" onblur="unblank(this)" onclick="aaa();"/>
<script type="text/javascript">
    document.getElementById('txtwebref').onkeypress = function checkMe(ev) {
        return !isNaN(this.value + String.fromCharCode(ev.keyCode) );
    }
</script>

document.getElementById('txtwebref')。onkeypress=函数checkMe(ev){
return!isNaN(this.value+String.fromCharCode(ev.keyCode));
}