Mozilla 中的按键问题

发布于 2024-08-28 06:49:51 字数 677 浏览 9 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间不值得 2024-09-04 06:49:51

你的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;

You javascript code is wrong

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;
}

You were taking the wrong element

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

is now changed to

var opinion = document.getElementById('opTxtArea').value;
千仐 2024-09-04 06:49:51

尝试删除“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。

Tried removing "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 Code:

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

In addition, at the line document.getElementById('opinion').value, I think you have the wrong ID entered.

难得心□动 2024-09-04 06:49:51

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

这里还有其他问题。你真正想要实现什么?该警报对于用户来说非常烦人,因此我建议改为在页面上显示一个元素来表示已超出字符限制。另外,当超过字符限制时,您希望发生什么?您可以阻止进一步输入(但不阻止删除),或者执行 Stack Overflow 对其注释字段所做的操作,即在超出字符限制时显示负数,但不阻止进一步输入。

As noted in another answer, you should remove the javascript: prefix in the onkeypress 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文