In JavaScript can I make a “click” event fire programmatically for a file input element?

前端 未结 28 1402
囚心锁ツ
囚心锁ツ 2020-11-21 06:29

I\'d like to make a click event fire on an tag programmatically.

Just calling click() doesn\'t seem to do anything or at lea

28条回答
  •  别那么骄傲
    2020-11-21 07:01

    I was researching this a while ago because I wanted to create a custom button that would open the file dialog and start the upload immediately. I just noticed something that might make this possible - firefox seems to open the dialog when you click anywhere on the upload. So the following might do it:

    1. Create a file upload and a separate element containing an image that you want to use as the button
    2. Arrange them to overlap and make the file element backgroud and border transparent so the button is the only thing visible
    3. Add the javascript to make IE open the dialog when the button/file input is clicked
    4. Use an onchange event to submit the form when a file is selected

    This is only theoretical since I already used another method to solve the problem but it just might work.

提交回复
热议问题