我正在用VS2012和Javascript开发一个地铁应用程序
我想重置我的文件输入的内容:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
我该怎么做呢?
我正在用VS2012和Javascript开发一个地铁应用程序
我想重置我的文件输入的内容:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
我该怎么做呢?
当前回答
我在ng2-file-upload中遇到过angular的问题。如果你正在寻找angular的解决方案,请参考下面的代码
HTML:
<输入type=“文件”/>
组件
import {Component, OnInit, ElementRef, ViewChild} from @angular/core;
@ViewChild (activeFrameinputFile) InputFrameVariable: ElementRef;
this.frameUploader.onSuccessItem =(项目,响应,状态,头)=> {
`this.`**InputFrameVariable**`.nativeElement.value = '';`
};
其他回答
<input type="file" id="mfile" name="mfile">
<p>Reset</p>
<script>
$(document).ready(function(){
$("p").click(function(){
$("#mfile").val('');
return false;
});
});
如果你有以下情况:
<input type="file" id="fileControl">
然后就这样做:
$("#fileControl").val('');
重置文件控件。
另一种解决方案(不选择HTML DOM元素)
如果你在输入中添加了'change'事件监听器,那么在javascript代码中你可以调用(对于某些特定的条件):
event.target.value = '';
例如在HTML中:
<input type="file" onChange="onChangeFunction(event)">
在javascript中:
onChangeFunction(event) {
let fileList = event.target.files;
let file = fileList[0];
let extension = file.name.match(/(?<=\.)\w+$/g)[0].toLowerCase(); // assuming that this file has any extension
if (extension === 'jpg') {
alert('Good file extension!');
}
else {
event.target.value = '';
alert('Wrong file extension! File input is cleared.');
}
有许多方法,我将建议去参考附件输入。
<input
id="image"
type="file"
required
ref={ref => this.fileInput = ref}
multiple
onChange={this.onFileChangeHandler}
/>
在提交后清除值。
this.fileInput.value = "";
也适用于动态控制。
Javascript
<input type="file" onchange="FileValidate(this)" />
function FileValidate(object) {
if ((object.files[0].size / 1024 / 1024) > 1) { //greater than 1 MB
$(object).val('');
}
}
JQuery
<input type="file" class="UpoloadFileSize">
$(document).ready(function () {
$('.UpoloadFileSize').bind('change', function () {
if ((this.files[0].size / 1024000) > 1.5) { //greater than 1.5 MB
$(this).val('');
alert('Size exceeded !!');
}
});
});