如何拒绝用户输入的额外字符?

发布于 2025-02-07 06:47:17 字数 502 浏览 2 评论 0原文

在我的html中,我有一系列输入。应允许用户在每个字符中输入一个字符。这是其html:

&lt; input class =“ green” type =“ text” maxlength =“ 1” /&gt; < /code>

css的CSS如下:

input {
    font: 1.5em "Times New Roman", Times, serif; 
    width:0.9em; height: 0.9em;
    text-align:center; }
.green { 
    color: green; }

如果用户输入角色,然后tries tries要输入另一个,什么都不会发生。但是,在我的设计中,用户可以输入任何数量的字符,即使将较早的字符推向左侧,只有最后一个字符才能看到。当焦点离开输入字段时,所有额外的字符都会消失,并且仅保留第一个输入字符。我如何确保当用户尝试输入其他字符时,它们会被忽略而什么都不会发生?

In my HTML, I have an array of inputs. The user should be allowed to enter only one character in each. Here is its HTML:

<input class = "green" type="text" maxlength="1" />

The CSS for this is as follows:

input {
    font: 1.5em "Times New Roman", Times, serif; 
    width:0.9em; height: 0.9em;
    text-align:center; }
.green { 
    color: green; }

If the user enters a character and then tries to enter another, nothing should happen. However, in my design the user can enter any number of characters, even though the earlier characters are pushed to the left and only the last character is visible. When the focus leaves the input field, all extra characters disappear and only the first input character remain. How can I make sure that when the user tries to enter additional characters, they are ignored and nothing happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

海拔太高太耀眼 2025-02-14 06:47:19

是您需要的吗?

function go(v){
    if (v.value.length==0){
    return true;
  }
    return false;
}
<input class = "green" type="text" maxlength="1" onkeydown="return go(this)"/>

Is it what you need?

function go(v){
    if (v.value.length==0){
    return true;
  }
    return false;
}
<input class = "green" type="text" maxlength="1" onkeydown="return go(this)"/>

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