我正在用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"/>
其他回答
你不能重置,因为它是一个只读文件。你可以克隆它来解决这个问题。
我的最佳解决方案
$(".file_uplaod_input").val(''); // this is working best ):
@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:
document.getElementById("uploadCaptureInputFile").value = "";
使用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试样
函数重置图像字段() { var $el = $('#uploadCaptureInputFile'); $el.wrap('<form>').closest('form').get(0).reset(); $el.解包(); } <script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <input type=“file” id=“uploadCaptureInputFile” class=“win-content colors” accept=“image/*” /> <按钮点击=“重置图像字段()”>重置字段</button>