如何在多项选择中保留现有的文件选择

How to keep existing file selections on multiple selections

本文关键字:选择 文件 保留      更新时间:2023-09-26

在html中,我们可以使用文件输入控件一次选择多个文件。但当我第二次选择文件时,以前的文件被新选择的文件覆盖。

我的问题是,我们可以在选择新文件时保留现有的文件选择吗?

我没有得到任何与此相关的片段,似乎大多数人都不需要这种功能。所以我自己做的。

  • 创建了一个文件输入控件
  • 单击此元素上的事件,使用jquery clone方法克隆具有重复id的当前文件输入。此克隆的输入控件设置为隐藏,当前输入控件保存最新文件
  • 所以,在提交表单时,我选择了所有文件

我怀疑这种方法在旧浏览器中的效果如何,但当前的chrome和firefox执行得很好。

I think your problem is that you give the same name to the different <input> tags.
<div id="selectFiles">
    <input type='file' name='userFile1'>
    <input type='file' name='userFile2'>
    <input type='file' name='userFile3'>
    <input type='file' name='userFile4'>
</div>