简体   繁体   English

当我克隆文件输入时,克隆的输入不会保留上载的文件。 原始输入

[英]When I clone a file input, the cloned input does not keep the uploaded file. The original input does

When I clone an input type file, It so happens that the file uploaded to the cloned input gets attached to the original input. 克隆输入类型文件时,碰巧上载到克隆输入的文件将附加到原始输入。 Its as if the cloned input is representing and taking attachments for the original. 就像克隆的输入代表原始附件一样。

Im cloning it the following way: clonedInput = $('input[type=file]').clone(true) is it posible to clone a file input and use it independently? 我用以下方式克隆它:clonedInput = $('input [type = file]')。clone(true)是否可以克隆文件输入并独立使用? or is there another way to solve this. 还是有另一种方法来解决这个问题。

On the dom, the user has the option to upload more than one file by clicking on a button that duplicates the first input, but the duplicate does not work because it attached the file to the original input. 在dom上,用户可以通过单击复制第一个输入的按钮来选择上传多个文件,但是复制不起作用,因为它会将文件附加到原始输入。

使用multiple属性以单个input type="file"元素上载多个文件

<input type="file" multiple />

Try giving the cloned item a unique ID with something like this: 尝试为克隆的项目赋予唯一的ID,如下所示:

clonedInput = $('input[type=file]').clone(true).attr("id",function(i,oldVal) {
            return oldVal.replace(/\d+/,function(m){
                return (+m + 1);
            });

解决该问题的方法是使用.prop(“ outerHTML”)将html转换为字符串,然后使用$ .parseHTML()将其转换回html元素,以便dom将其视为全新的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM