gpt4 book ai didi

javascript - 如何删除输入文件中的附件? (火狐)

转载 作者:行者123 更新时间:2023-11-29 19:26:38 28 4
gpt4 key购买 nike

如何从 <input type="file"> 中删除选定的附件元素 ?
当您已经选择一个附件时,Firefox 似乎无法删除附件。

我尝试删除名称并点击打开,但没有任何反应。
我也试过取消,但附件还在。

我的解决方案是添加一些 onclick javascript 来删除该值,但是有没有办法像 chrome 的附件一样不添加一些脚本?

Firefox screenshot

最佳答案

不,我认为您将不得不插入一些 js。

Firefox 似乎将文件输入的值保存在缓存中(顺便说一句,就像任何其他输入值一样),并且仅在添加新值时才更新它。

取消按钮并没有清除它,这在我对英语的理解中是有道理的,因为如果我想“取消”选择另一个文件的 Action ,并不意味着我想“清除”我的那个以前有过。

如果你真的希望你的用户能够清除那些,你可以添加一些按钮来完成

var f = document.querySelectorAll('input[type=file]'),
clearInput = function(){this.previousSibling.value = '';};
for(var i = 0; i < f.length; i++){
button = document.createElement('button');
button.textContent = 'clear';
button.addEventListener('click', clearInput);
f[i].parentNode.insertBefore(button, f[i].nextSibling);
}
<input type="file"/><br>
<input type="file"/><br>
<input type="file"/><br>

或者每次他搜索新文件时清除它。

var f = document.querySelectorAll('input[type=file]');
var clearInput = function(){this.value=''};
for(var i=0;i<f.length; i++)
f[i].addEventListener('click', clearInput);
<input type="file"/><br>
<input type="file"/><br>
<input type="file"/><br>

关于javascript - 如何删除输入文件中的附件? (火狐),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com