可以选择其生成的元素组件吗?

时间:2016-09-25 14:51:11

标签: javascript reactjs react-native

我有这个组件来渲染一个表单,但同样它的形式是“catcher”

var FormUpload = React.createClass({
                    submit : function(){
                     var formdata =new FormData();
                     // I want to select form that generated by this self component.
                },

                render : function(){
                    return (
                    <form id="form" ref="uploadForm" className="uploader" encType="multipart/form-data">
                    </form>
                    );
                }
            });

1 个答案:

答案 0 :(得分:0)

来自docs

  

如果你需要强制执行一项操作,你必须获得一个   对DOM节点的引用。例如,如果你想强制性地   提交表格,一种方法是附上表格的参考   元素并手动调用form.submit()。

submit : function() {
    var formdata = new FormData();
    this.form.submit(); // access reference here                   
},

render : function() {
    return (
        <form id="form" ref="uploadForm" 
            className="uploader" 
            encType="multipart /form-data"
            ref={function(form) { //add reference here
                this.form = form;
            }}
        >
        </form>
    );
}