如何通过另一个DOM触发INPUT FILE事件REACTJS [英] How to trigger INPUT FILE event REACTJS by another DOM
问题描述
我有一个输入按钮和输入文件,我想单击按钮,它将触发输入文件 REACT JS中的事件.
I have a INPUT BUTTON and INPUT FILE, I want to click the BUTTON and it will trigger the INPUT FILE event in REACT JS.
React.createElement('input',{type:'file', name:'myfile'})
然后点击按钮
React.createElement('a',{onClick: this.doClick},'Select File')
那么当我们单击A HREF时,如何定义和触发INPUT FILE click事件?
So how to define and trigger the INPUT FILE click event when we click the A HREF?
感谢您的帮助.:-)
推荐答案
编辑:这是我很久以前回答的一个问题,目前尚不十分清楚.有趣的是,它被认为是有效的^^.
This is a question I answered a long time ago not knowing very much react at this time. The fun thing is that it has been considered valid ^^.
因此,对于阅读此答案的任何人;这个答案是错误的,是一个很好的例子,说明您不应做出反应.
So for anyone reading this answer; this answer is wrong and is a very good example of something you shouldn't do in react.
请再次在下面找到一个不错的反模式,不要这样做.
==================================================
=================================================
您可以使用jQuery实现此目标:
You can achieve this using jQuery:
this.doClick: function() {
$('input[type=file]').trigger('click');
}
React不提供触发事件的特定功能,您可以使用jQuery或仅使用本机Javascript:请参见
React does not provide specific functions to trigger events, you can use jQuery or simply native Javascript: see Creating and triggering events on MDN
这篇关于如何通过另一个DOM触发INPUT FILE事件REACTJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!