How to trigger INPUT FILE event REACTJS by another DOM

后端 未结 6 1008
-上瘾入骨i
-上瘾入骨i 2021-01-02 01:53

I have a INPUT BUTTON and INPUT FILE, I want to click the BUTTON and it will trigger the INPUT FILE event

6条回答
  •  佛祖请我去吃肉
    2021-01-02 02:24

    You don't need jQuery for this. You don't even need an event handler. HTML has a specific element for this, called label.

    First, make sure your input element has an id attribute:

    React.createElement('input',{type:'file', name:'myfile', id:'myfile'})
    

    Then, instead of:

    React.createElement('a',{onClick: this.doClick},'Select File')
    

    Try:

    React.createElement('label',{htmlFor: 'myfile'},'Select File')
    

    (Instead of adding htmlFor and id attributes, another solution is to make the input element a child of the label.)

    Now clicking the label should trigger the same behaviour as clicking the input itself.

提交回复
热议问题