如何在不丢失事件侦听器和展开属性的情况下清除文件输入

How to clear file input without lose event listeners and expanded properties?

本文关键字:属性 情况下 清除 输入 文件 失事 侦听器 事件      更新时间:2023-09-26

我想在我的表单中清除文件输入,但我使用AngularJS和其他JQuery组件,我不能失去事件侦听器和扩展属性。

有一种方法可以做到不删除或使用JQuery replaceWithclone函数?

如何设置它的value属性为空字符串?

function clearFile() {
  document.getElementById("file").value = "";
}
<input type="file" name="" id="file" />
<input type="button" value="clear" onclick="clearFile();" />

您可以用它的克隆文件替换文件输入;

$('#delImg').on('click', function() {
   $('body #exampleInputFile').replaceWith($('body #exampleInputFile').val('').clone(true));
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
  
<button type="button" id="delImg" class="btn btn-default btn-hover-green" data-action="save" data-dismiss="modal" role="button">Delete</button>
</div>