Jes*_*sus 361 javascript winjs visual-studio-2012
我正在使用VS2012和Javascript开发一个metro应用程序
我想重置文件输入的内容:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
jak*_*lla 329
@ dhaval-marthak在评论中发布的jQuery解决方案显然有效,但如果你看看实际的jQuery调用,很容易看到jQuery正在做什么,只需将value
属性设置为空字符串即可.所以在"纯粹的"JavaScript中它将是:
document.getElementById("uploadCaptureInputFile").value = "";
Run Code Online (Sandbox Code Playgroud)
lev*_*ter 77
您需要包装<input type = “file”>
到<form>
标签,然后您可以通过重置您的表单重置输入:
onClick="this.form.reset()"
Run Code Online (Sandbox Code Playgroud)
Max*_*mus 65
这是最佳解决方案:
input.value = ''
if(!/safari/i.test(navigator.userAgent)){
input.type = ''
input.type = 'file'
}
Run Code Online (Sandbox Code Playgroud)
在这里的所有答案中,这是最快的解决方案.没有输入克隆,没有表单重置!
我在所有浏览器中检查过它(它甚至支持IE8).
pix*_*ker 43
您可以克隆它并将其替换为自身,所有事件仍然附加:
<input type="file" id="control">
Run Code Online (Sandbox Code Playgroud)
和
var input = $("#control");
function something_happens() {
input.replaceWith(input.val('').clone(true));
};
Run Code Online (Sandbox Code Playgroud)
谢谢:Css-tricks.com
sst*_*oss 39
如果您有以下内容:
<input type="file" id="fileControl">
Run Code Online (Sandbox Code Playgroud)
然后就是:
$("#fileControl").val('');
Run Code Online (Sandbox Code Playgroud)
重置文件控件.
ind*_*eed 38
另一种解决方案(不选择HTML DOM元素)
如果您在该输入上添加了'change'事件侦听器,那么在javascript代码中您可以调用(对于某些指定的条件):
event.target.value = '';
Run Code Online (Sandbox Code Playgroud)
例如在HTML中:
<input type="file" onChange="onChangeFunction(event)">
Run Code Online (Sandbox Code Playgroud)
并在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.');
}
Run Code Online (Sandbox Code Playgroud)
Gyr*_*com 29
以下代码适用于jQuery.它适用于每个浏览器,并允许保留事件和自定义属性.
var $el = $('#uploadCaptureInputFile');
$el.wrap('<form>').closest('form').get(0).reset();
$el.unwrap();
Run Code Online (Sandbox Code Playgroud)
有关代码和演示,请参阅此jsFiddle.
有关更多信息,请参见如何使用JavaScript重置文件输入.
Ali*_*eza 23
使用纯JavaScript重置文件上传按钮非常简单!
有几种方法可以做到这一点,但在许多浏览器中运行良好的必须直接的方法是将字段值更改为空,这样上传字段被重置,也尝试使用纯javascript而不是任何框架,我创建了下面的代码,只需检查出来(ES6):
function resetFile() {
const file = document.querySelector('.file');
file.value = '';
}
Run Code Online (Sandbox Code Playgroud)
<input type="file" class="file" />
<button onclick="resetFile()">Reset file</button>
Run Code Online (Sandbox Code Playgroud)
ans*_*son 11
我用于vuejs
<input
type="file"
ref="fileref"
@change="onChange"/>
this.$refs.fileref.value = ''
Run Code Online (Sandbox Code Playgroud)
P S*_*tro 11
特别是对于Angular
document.getElementById('uploadFile').value = "";
Run Code Online (Sandbox Code Playgroud)
可以,但是如果使用Angular,它将说“类型'HTMLElement'.any上不存在属性'值'”
document.getElementById()返回不包含value属性的HTMLElement类型。因此,将其转换为HTMLInputElement
(<HTMLInputElement>document.getElementById('uploadFile')).value = "";
Run Code Online (Sandbox Code Playgroud)
额外:-
但是,我们不应该在angular中使用document.xyz()DOM操作。
为此,角度提供了@ VIewChild,@ ViewChildren等,它们分别是document.querySelector()和document.queryselectorAll()。
甚至我都没看过。更好地跟随专家的博客
重置输入文件非常单一
$('input[type=file]').val(null);
如果绑定重置文件,请更改表单的其他字段,或使用ajax加载表单.
此示例适用
例如,选择器是$('input[type=text]')
表单的任何元素
事件 click
,change
或任何事件
$('body').delegate('event', 'selector', function(){
$('input[type=file]').val(null) ;
});
Run Code Online (Sandbox Code Playgroud)
我想念这里的另一个解决方案 (2021):我FileList
用来与文件输入进行交互。
由于无法创建FileList
对象,因此我使用DataTransfer
, 这会带来干净的 FilleList 。
我用以下方法重置它:
file_input.files=new DataTransfer().files
Run Code Online (Sandbox Code Playgroud)
就我而言,这非常适合,因为我仅通过FileList
与封装的文件输入元素进行交互。
有很多方法,我建议使用 ref 附件来输入。
<input
id="image"
type="file"
required
ref={ref => this.fileInput = ref}
multiple
onChange={this.onFileChangeHandler}
/>
Run Code Online (Sandbox Code Playgroud)
提交后清除值。
this.fileInput.value = "";
Run Code Online (Sandbox Code Playgroud)
小智 7
我最好的解决方案
$(".file_uplaod_input").val(''); // this is working best ):
Run Code Online (Sandbox Code Playgroud)
使用IE 10我解决了以下问题:
var file = document.getElementById("file-input");
file.removeAttribute('value');
file.parentNode.replaceChild(file.cloneNode(true),file);
Run Code Online (Sandbox Code Playgroud)
其中:
<input accept="image/*" onchange="angular.element(this).scope().uploadFile(this)" id="file-input" type="file"/>
Run Code Online (Sandbox Code Playgroud)
document.getElementById("uploadCaptureInputFile").value = "";
Run Code Online (Sandbox Code Playgroud)
使用 Angular,您可以创建一个模板变量并将值设置为null
<input type="file" #fileUploader />
<button (click)="fileUploader.value = null">Reset from UI</button>
Run Code Online (Sandbox Code Playgroud)
或者您可以创建一个像这样重置的方法
组件.ts
@ViewChild('fileUploader') fileUploader:ElementRef;
resetFileUploader() {
this.fileUploader.nativeElement.value = null;
}
Run Code Online (Sandbox Code Playgroud)
模板
<input type="file"/>
<button (click)="resetFileUploader()">Reset from Component</button>
Run Code Online (Sandbox Code Playgroud)
<input type="file" id="mfile" name="mfile">
<p>Reset</p>
<script>
$(document).ready(function(){
$("p").click(function(){
$("#mfile").val('');
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
432278 次 |
最近记录: |