我正在用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/*" />
我该怎么做呢?
当前回答
重置文件上传按钮是如此容易使用纯JavaScript!
有几个方法可以做到这一点,但必须直接的方法,这是在许多浏览器工作良好的改变文件值为零,这样上传文件得到重置,也尝试使用纯javascript而不是任何框架,我创建了下面的代码,检查一下(ES6):
函数resetFile() { const file = document.querySelector('.file'); 文件。Value = "; } <input type="file" class="file" /> <按钮onclick = " resetFile ()“>重置文件> < /按钮
其他回答
var fileInput = $('#uploadCaptureInputFile');
fileInput.replaceWith(fileInput.val('').clone(true));
使用angular,你可以创建一个模板变量,并将其值设置为null
<input type="file" #fileUploader />
<button (click)="fileUploader.value = null">Reset from UI</button>
或者你可以像这样创建一个方法来重置
component.ts
@ViewChild('fileUploader') fileUploader:ElementRef;
resetFileUploader() {
this.fileUploader.nativeElement.value = null;
}
模板
<input type="file"/>
<button (click)="resetFileUploader()">Reset from Component</button>
stackblitz试样
我用来做vuejs
<input
type="file"
ref="fileref"
@change="onChange"/>
this.$refs.fileref.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 !!');
}
});
});
这是最好的解决方案:
input.value = ''
if(!/safari/i.test(navigator.userAgent)){
input.type = ''
input.type = 'file'
}
在所有的答案中这是最快的解。没有输入克隆,没有窗体重置!
我在所有浏览器中都检查了它(它甚至支持IE8)。