从.clone()输入空文件 [英] Empty file input from .clone()
本文介绍了从.clone()输入空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试克隆文件输入表单,每次我选择一个文件然后点击添加更多来克隆文件输入,但它已经在输入中复制了所选文件。
I'm trying to clone a file input form, which every time I select a file then click "add more" to clone the file input, but it has copied the selected file in the input.
<input type="file" />
<span id="add-more-files">Add more</span>
jQuery:
$('#add-more-files').click(function()
{
var cloned = $(this).prev().clone();
$(cloned).insertBefore($(this));
});
演示: jsFiddle 。
现在无论何时在克隆前一个文件之前选择文件,你都可以看到它选择了相同的文件,我需要它克隆时为空。
Now whenever you select a file before cloning the previous, you can see it has the same file selected, I need it to be empty when cloned.
编辑:
我怎样才能克隆这个,参考 Neal的回答?
How would I be able to clone this, referring to Neal's answer?
<div class="wrap"><input type="file /></div>
<span id="add-more-files">Add more</span>
推荐答案
您可以尝试的是:
$('#add-more-files').click(function()
{
var cloned = $(this).prev().clone();
cloned.val(null);
$(cloned).insertBefore($(this));
});
更新
或者如果所有输入都是以前的副本,只需创建一个新的而不是克隆:
UPDATE
or if all the inputs will be copies from the previous just create a new one instead of cloning:
$('#add-more-files').click(function()
{
var cloned = $(this).prev();
$('<input>',{type:cloned.attr('type')}).insertBefore($(this));
});
这篇关于从.clone()输入空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文