简体   繁体   English

从 Javascript 更改 React Textarea 的值后触发事件

[英]Fire Events after change of React Textarea's value from Javascript

I got a little Android App with a webview.我有一个带有 webview 的小 Android 应用程序。 I want to set a textareas value from there, which seems to be a react object.我想从那里设置一个 textareas 值,这似乎是 object 的反应。

It's working so far, I got the handle on the textarea and can set the value by my own javascript-routine.到目前为止它正在工作,我得到了 textarea 的句柄,并且可以通过我自己的 javascript 例程设置值。 But the triggers are not called, so the form validation gives me an error the field would be empty.但是没有调用触发器,所以表单验证给了我一个错误,该字段将为空。

I can manipulate the textarea with我可以用

document.getElementById('textareaId').value = "new value";

(I found the element by its properties and set the id myself for later use, so no need to tell react possibly isn't using ids) (我通过它的属性找到了元素并自己设置了 id 以供以后使用,所以不需要告诉 react 可能没有使用 ids)

How can I fire the events on the react objects?如何触发反应对象上的事件?

I already tried to fire the events on the textarea itself but that doesn't seem to work.我已经尝试在 textarea 本身上触发事件,但这似乎不起作用。 In the browser I see triggers onchange, onkeyup, onkeydown but I can't call them from outside, nor can I display them except from the browsers dev tools.在浏览器中,我看到触发器onchange, onkeyup, onkeydown但我不能从外部调用它们,也不能从浏览器开发工具中显示它们。

Any workaround for this would also be very welcome!对此的任何解决方法也将非常受欢迎!

Seems to be just a matter of the right type of event:似乎只是正确类型的事件的问题:

var evt = document.createEvent("Events");            
evt.initEvent("change", true, true);                
textarea.dispatchEvent(evt);

from this answer simulate keyboard input / insert string into textarea (adwords)从这个答案模拟键盘输入/将字符串插入文本区域(adwords)

did the trick.成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM