我正在用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/*" />
我该怎么做呢?
当前回答
在IE 10中,我解决了这个问题:
var file = document.getElementById("file-input");
file.removeAttribute('value');
file.parentNode.replaceChild(file.cloneNode(true),file);
地点:
<input accept="image/*" onchange="angular.element(this).scope().uploadFile(this)" id="file-input" type="file"/>
其他回答
也适用于动态控制。
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)。
你可以克隆它,用它自己替换它,所有事件仍然附加:
<input type="file" id="control">
and
var input = $("#control");
function something_happens() {
input.replaceWith(input.val('').clone(true));
};
谢谢:csstricks.com
另一种解决方案(不选择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.');
}
我用来做vuejs
<input
type="file"
ref="fileref"
@change="onChange"/>
this.$refs.fileref.value = ''