我正在用VS2012和Javascript开发一个地铁应用程序

我想重置我的文件输入的内容:

<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />

我该怎么做呢?


当前回答

这是最好的解决方案:

input.value = ''

if(!/safari/i.test(navigator.userAgent)){
  input.type = ''
  input.type = 'file'
}

在所有的答案中这是最快的解。没有输入克隆,没有窗体重置!

我在所有浏览器中都检查了它(它甚至支持IE8)。

其他回答

@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:

document.getElementById("uploadCaptureInputFile").value = "";

函数重置图像字段() { 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>

你应该试试这个:

$("#uploadCaptureInputFile").val('');

我用来做vuejs

   <input
          type="file"
          ref="fileref"
          @change="onChange"/>

this.$refs.fileref.value = ''

重置文件上传按钮是如此容易使用纯JavaScript!

有几个方法可以做到这一点,但必须直接的方法,这是在许多浏览器工作良好的改变文件值为零,这样上传文件得到重置,也尝试使用纯javascript而不是任何框架,我创建了下面的代码,检查一下(ES6):

函数resetFile() { const file = document.querySelector('.file'); 文件。Value = "; } <input type="file" class="file" /> <按钮onclick = " resetFile ()“>重置文件> < /按钮