Javascript Mozilla中的按键问题

Javascript Mozilla中的按键问题,javascript,events,mozilla,Javascript,Events,Mozilla,我正在使用TextArea获取一些输入。一个标签显示左侧更新的字符。 它在IE中工作正常,但在FF3.0中,在达到最大限制后,它不允许删除或退格键 我在textarea的按键事件上使用javascript函数 javascript代码是 function checkLength() { var opinion = document.getElementById('opinion').value; if(opinion.length > 50) alert("

我正在使用TextArea获取一些输入。一个标签显示左侧更新的字符。 它在IE中工作正常,但在FF3.0中,在达到最大限制后,它不允许删除或退格键

我在textarea的按键事件上使用javascript函数

javascript代码是

function checkLength()
{
    var opinion = document.getElementById('opinion').value;
    if(opinion.length > 50)
        alert("You have reached the mas limit.");
    else
        document.getElementById('limit').innerHTML = 50 - opinion.length;
}
在页面上,我正在使用这个

<label id="limit">50 </label>
<textarea id="opTxtArea" onkeypress="javascript:checkLength();"></textarea>

一切正常。问题出现在FF中,当输入达到最大限制时,会显示消息,但不允许删除或退格。

您的javascript代码是错误的

function checkLength()
{
    var opinion = document.getElementById('opTxtArea').value;
    if(opinion.length > 50)
        alert("You have reached the mas limit.");
    else
        document.getElementById('limit').innerHTML = 50 - opinion.length;
}
你选错元素了

var opinion = document.getElementById('opinion').value;
现在改为

var opinion = document.getElementById('opTxtArea').value;

是否尝试删除javascript:

Javascript:

function checkLength()
{
var opinion = document.getElementById('opTxtArea').value;
if(opinion.length > 50)
    alert("You have reached the max limit.");
else
    document.getElementById('limit').innerHTML = 50 - opinion.length;
}
HTML代码:

<label id="limit">50</label>
<textarea id="opTxtArea" onkeypress="checkLength();" maxlength="50"></textarea>

此外,在document.getElementById'opinion'.value行,我认为您输入了错误的ID。

如另一个答案中所述,您应该删除onkeypress属性中的javascript:前缀,因为这没有任何用途


这里还有其他问题。你到底想要实现什么?该警报对于用户来说非常烦人,因此我建议在页面上显示一个元素,表示已超过字符限制。另外,当超过字符限制时,您希望发生什么情况?您可以在不阻止删除的情况下阻止进一步输入,也可以像堆栈溢出对其注释字段所做的那样,在超过字符限制时显示负数,但不阻止进一步输入。

但它仍然不能解决Mozilla中的问题。达到限制后,不允许退格或删除.Thx进行回复。那是个错误。更正正确的id后,问题仍然存在。