设置 value 属性后,如何使 textarea 像用户输入一样更新?

发布于 2025-01-15 04:23:28 字数 760 浏览 4 评论 0原文

我正在构建一个 Chrome 扩展程序,使用户能够将预设回复插入其频道上的 Youtube 评论中。我使用 textarea 的 value 属性插入文本:

getHighlightedComment().querySelector("textarea").value = cannedResponseText

问题是,textarea 不会调整其大小,并且提交按钮不会启用,直到我手动输入另一个字母

输入图片此处描述

在此处输入图像描述

我安装了一个不同的扩展,它具有类似的功能,但没有这些问题,所以这一定是可能的。

我已经尝试手动发送 keypress/keydown/keyup 事件,但它们似乎无法在 textarea 内工作>s。没有任何反应发生。

I'm building a Chrome extension that enables the user to insert canned responses into Youtube comments on their channel. I insert the text using textarea's value property:

getHighlightedComment().querySelector("textarea").value = cannedResponseText

The problem is, the textarea doesn't adapt its size and the submit button doesn't get enabled until I type in another letter manually.

enter image description here

enter image description here

I have a different extension installed that has a similar functionality that doesn't have these problems, so it must be possible.

I already tried sending keypress/keydown/keyupevents manually, but they seem to not work inside textareas. Nothing happens in response to them.

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

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

发布评论

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

评论(1

樱娆 2025-01-22 04:23:28

我想您需要在实际更改后在您的字段上触发一些事件这里是您可以如何处理它。

UPD。

我尝试发送 keypress/keydown/keyup 事件,但似乎它们在文本区域内不起作用。什么也没发生。

我想这更多是关于 onchange 事件。

此外,您还需要检查文本区域的 CSS - 行未硬编码,高度和最大高度未硬编码(值为“auto”)。

以下是一些使用 JS 更新它的解决方法 - https://stackoverflow.com/a/25621277/9994697

I guess you need to trigger some event on your field after actual change Here is how you can deal with it.

UPD.

I tried sending keypress/keydown/keyup events but it seems they don't work inside textareas. Nothing happens.

I guess it is more about onchange event.

Also you need to check the CSS of your textarea - rows are not hardcoded, height and max-height is not hardcoded (has value 'auto').

Here are some workarounds to update it with JS - https://stackoverflow.com/a/25621277/9994697

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