HTML 文件选择事件

时间:2021-05-15 00:02:54

Let's say we have this code:

假设我们有这个代码:

<form action='' method='POST' enctype='multipart/form-data'>
    <input type='file' name='userFile'><br>
    <input type='submit' name='upload_btn' value='upload'>
</form>

which results in this:

导致:

HTML 文件选择事件

When the user clicks the 'Browse...' button, a file search dialog box is opened:

当用户点击“浏览…”“按钮,打开文件搜索对话框:

HTML 文件选择事件

The user will select the file either by double-clicking the file or by clicking the 'Open' button .

用户可以通过双击文件或单击“打开”按钮来选择文件。

Is there a Javascript Event that I can use to be notified after after the file is selected?

是否有一个Javascript事件可以在文件被选中后通知我?

5 个解决方案

#1


132  

Listen to the change event.

请收听更改事件。

input.onchange = function(e) { 
  ..
};

#2


30  

When you have to reload the file, you can erase the value of input. Next time you add a file, 'on change' event will trigger.

当您必须重新加载该文件时,您可以删除输入的值。下次添加文件时,“on change”事件将触发。

document.getElementById('my_input').value = null;
// ^ that just erase the file path but do the trick

#3


7  

jQuery way:

jQuery:

$('input[name=myInputName]').change(function(ev) {

    // your code
});

#4


1  

That's the way I did it with pure JS:

这就是我用纯JS做的:

var files = document.getElementById('filePoster');
var submit = document.getElementById('submitFiles');
var warning = document.getElementById('warning');
files.addEventListener("change", function () {
  if (files.files.length > 10) {
    submit.disabled = true;
    warning.classList += "warn"
    return;
  }
  submit.disabled = false;
});
#warning {
    text-align: center;
}

#warning.warn {
	color: red;
	transform: scale(1.5);
	transition: 1s all;
}
<section id="shortcode-5" class="shortcode-5 pb-50">
    <p id="warning">Please do not upload more than 10 images at once.</p>
    <form class="imagePoster" enctype="multipart/form-data" action="/gallery/imagePoster" method="post">
        <div class="input-group">
  	    <input id="filePoster" type="file" class="form-control" name="photo" required="required" multiple="multiple" />
	    <button id="submitFiles" class="btn btn-primary" type="submit" name="button">Submit</button>
        </div>
    </form>
</section>

#5


0  

The Change event gets called even if you click on cancel..

即使您单击cancel,更改事件也会被调用。

#1


132  

Listen to the change event.

请收听更改事件。

input.onchange = function(e) { 
  ..
};

#2


30  

When you have to reload the file, you can erase the value of input. Next time you add a file, 'on change' event will trigger.

当您必须重新加载该文件时,您可以删除输入的值。下次添加文件时,“on change”事件将触发。

document.getElementById('my_input').value = null;
// ^ that just erase the file path but do the trick

#3


7  

jQuery way:

jQuery:

$('input[name=myInputName]').change(function(ev) {

    // your code
});

#4


1  

That's the way I did it with pure JS:

这就是我用纯JS做的:

var files = document.getElementById('filePoster');
var submit = document.getElementById('submitFiles');
var warning = document.getElementById('warning');
files.addEventListener("change", function () {
  if (files.files.length > 10) {
    submit.disabled = true;
    warning.classList += "warn"
    return;
  }
  submit.disabled = false;
});
#warning {
    text-align: center;
}

#warning.warn {
	color: red;
	transform: scale(1.5);
	transition: 1s all;
}
<section id="shortcode-5" class="shortcode-5 pb-50">
    <p id="warning">Please do not upload more than 10 images at once.</p>
    <form class="imagePoster" enctype="multipart/form-data" action="/gallery/imagePoster" method="post">
        <div class="input-group">
  	    <input id="filePoster" type="file" class="form-control" name="photo" required="required" multiple="multiple" />
	    <button id="submitFiles" class="btn btn-primary" type="submit" name="button">Submit</button>
        </div>
    </form>
</section>

#5


0  

The Change event gets called even if you click on cancel..

即使您单击cancel,更改事件也会被调用。