admin管理员组

文章数量:1330619

I have a form which I'm using to upload files. In current situation if a user choose an image from his puter he have to click button upload to upload the image. I'm trying to find a way to skip the step with a button pressing.

How to call a javascript function when the file is selected from user ?

I have a form which I'm using to upload files. In current situation if a user choose an image from his puter he have to click button upload to upload the image. I'm trying to find a way to skip the step with a button pressing.

How to call a javascript function when the file is selected from user ?

Share Improve this question edited Feb 18, 2011 at 9:47 Bobby 11.6k5 gold badges47 silver badges70 bronze badges asked Feb 18, 2011 at 9:46 brodobrodo 651 gold badge2 silver badges5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The onchange event is fired when a user specify a file for the upload filed. You could go about something like this:

<input type="file" name="someName" id="uploadID" />

Javascript:

var el = document.getElementById('#uploadID');
el.onchange = function(){
  // your code...
};

However, javascript validation is good idea but make sure that you do the actual validation on the server-side :)

Using the example above...

<input type="file" name="someName" id="uploadID" />

JavaScript

document.getElementById('uploadID').addEventListener('change', () => {
//Your code...
});

本文标签: javascriptCall function on file uploadStack Overflow