检测文本区域中的返回键,如果存在某个字符串,则向文本区域写入一行 [javascript]

发布于 2024-09-18 11:35:34 字数 222 浏览 3 评论 0原文

基本上有一个大的文本区域,我希望能够用它做一些事情;

  1. 检测用户何时按“enter”键转到新行,

  2. 当按 Enter 时,如果该行包含某个字符串(比如“hello”),则会写入一行到显示“hello to you”的文本区域。

我一生都无法检测文本区域中的字符串。不过,我是个大新手。

多谢。

Basically have a large textarea, and I want to be able to do a few things with it;

  1. Detect when the user presses "enter" to go to a new line,

    and

  2. When enter is pressed, if the line contains a certain string let's say "hello", a line would be written to the textarea that reads "hello to you."

I cannot, for the life of me, detect a string from within a textarea. I am a huge newb, though.

Much obliged.

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

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

发布评论

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

评论(1

水波映月 2024-09-25 11:35:34

为此,我将使用 jQuery 这样的 JavaScript 框架。代码看起来像这样:

$(function() {
    $('textarea').keypress(function(event) {
        if (event.which == 13) { // Return key
            var textareaText = $(this).val();
            if (textareaText.match(/hello/)) {
                $(this).val(textareaText+"\nhello to you.");
            }
        }
    });
});

I would use a JavaScript framework like jQuery for this purpose. The code would look something like this:

$(function() {
    $('textarea').keypress(function(event) {
        if (event.which == 13) { // Return key
            var textareaText = $(this).val();
            if (textareaText.match(/hello/)) {
                $(this).val(textareaText+"\nhello to you.");
            }
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文