如何在 JavaScript 中的表单中每三个字符添加一个空格并具有替换功能?

发布于 2024-12-29 15:40:54 字数 478 浏览 2 评论 0原文

如何在 JavaScript 中的表单中每三个字符添加一个空格并具有替换功能?我已经看到了这个问题,但我对此仍然不清楚并且是新的,并且我不确定代码将进入脚本的何处。这是我到目前为止所拥有的。请具体说明。

    <script type="text/javascript">

    function var myfunction()
    {
    }

    </script>

    </head>
    <body>

    <FORM ACTION="rules.html" NAME="form">
    <INPUT TYPE=TEXT NAME="inp" SIZE=40 onkeyup="this.value=this.value.replace('t','A').replace('a','U')">
    </FORM>

    </script>

How do I add a space every three characters in a form in JavaScript and have a replace function? I've seen the question, but I'm still unclear and new at this and I'm not sure where the code would go into the script. This is what I have so far. Please be specific.

    <script type="text/javascript">

    function var myfunction()
    {
    }

    </script>

    </head>
    <body>

    <FORM ACTION="rules.html" NAME="form">
    <INPUT TYPE=TEXT NAME="inp" SIZE=40 onkeyup="this.value=this.value.replace('t','A').replace('a','U')">
    </FORM>

    </script>

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

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

发布评论

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

评论(1

放我走吧 2025-01-05 15:40:54

在我看来,这就像你试图进入 RNA 并让它在进入时自动分裂成三联体。但即使这不是您正在做的,这也应该能满足您的需求。

首先,删除表单下方多余的结束 标记。

然后,将表单中的 input 替换为以下内容:

<input type="text" name="inp" size="40" onkeyup="this.value=myfunction(this.value);">

并在 myfunction 函数中使用以下内容:

function myfunction(val) {
    val = val.replace('t','A').replace('a','U');
    val = val.replace(/(\w{3})$/, '$1 ');
    return val;
}

当然,如果您将 javascript 从标记中提取出来,那么这一切都会变得更清晰,并且稍微清理一下你的 HTML,但这应该能让你继续前进。

It looks to me like your trying to enter in RNA and have it automatically split out into triplets on entry. But even if that's not what you're doing, this should get you what you need.

First, remove the extra closing </script> tag you have below your form.

Then, replace the input in your form with the following:

<input type="text" name="inp" size="40" onkeyup="this.value=myfunction(this.value);">

and in the myfunction function use this:

function myfunction(val) {
    val = val.replace('t','A').replace('a','U');
    val = val.replace(/(\w{3})$/, '$1 ');
    return val;
}

Granted this could all be cleaner if you pulled your javascript out of your markup and cleaned up your HTML a little, but this should get you going.

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