Mozilla 中的按键问题
我正在使用textarea
来获取一些输入。标签显示剩余的更新字符。 在IE中工作正常,但在FF 3.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中,当输入达到最大限制时,会显示消息,但不允许删除或退格。
I am usingtextarea
to get some inputs. A label shows the updated chars left.
It works fine in IE, but in FF 3.0, after reaching the max limit, it doesn't allow to delete or backspace key.
I am using a javascript function on keypress event of the textarea
.
the javascript code is
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;
}
while on the page, i am using this
<label id="limit">50 </label>
<textarea id="opTxtArea" onkeypress="javascript:checkLength();"></textarea>
Everything is working fine. The problem arises in FF, when the inputs reach the max limit, the message is displayed, but it doesn't allow to delete or backspace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的javascript代码是错误的
你采取了错误的元素
现在改为
You javascript code is wrong
You were taking the wrong element
is now changed to
尝试删除“javascript:”?
Javascript:
HTML 代码:
此外,在
document.getElementById('opinion').value
行,我认为您输入了错误的 ID。Tried removing "javascript:"?
Javascript:
HTML Code:
In addition, at the line
document.getElementById('opinion').value
, I think you have the wrong ID entered.正如另一个答案中所述,您应该删除
onkeypress
属性中的javascript:
前缀,因为这没有任何作用。这里还有其他问题。你真正想要实现什么?该警报对于用户来说非常烦人,因此我建议改为在页面上显示一个元素来表示已超出字符限制。另外,当超过字符限制时,您希望发生什么?您可以阻止进一步输入(但不阻止删除),或者执行 Stack Overflow 对其注释字段所做的操作,即在超出字符限制时显示负数,但不阻止进一步输入。
As noted in another answer, you should remove the
javascript:
prefix in theonkeypress
attribute, since this serves no purpose.There are other problems here. What do you actually want to achieve? The alert is very annoying for the user, so I would suggest instead displaying an element on the page to say that the character limit has been exceeded. Also, what do you want to happen when the character limit is exceeded? You could either prevent further input (while not preventing deletion) or do what Stack Overflow does for its comment fields, which is to show a negative number when the character limit has been exceeded but not prevent further input.