Javascript 复制上载字段也会复制该值
我有以下jquery函数:Javascript 复制上载字段也会复制该值,javascript,jquery,html,Javascript,Jquery,Html,我有以下jquery函数: makeupload = function() { if (filecount == 7) { alert('You can Only upload up to 10 files'); } else { filecount++; var el = $('div#fileupload'); el = el[el.length - 1]; el = $(el);
makeupload = function() {
if (filecount == 7) {
alert('You can Only upload up to 10 files');
} else {
filecount++;
var el = $('div#fileupload');
el = el[el.length - 1];
el = $(el);
var nel = el.clone();
nel.val('');
var fi = nel.find("#fileinput");
fi.attr("name", "file[]");
fi.val('');
el.after(nel);
nel.show();
fi.bind("change", function(e) {
makeupload();
});
}
}
makeupload();
在firefox中,它只复制最后一个字段的值,因此,如果我浏览一个图像,然后使用上面的函数添加一个新字段,它会复制字段内的值以创建新字段
以下是html文件:
<div id="fileupload" style=""><input type="file" name="file[]" /><br /></div>
我不知道你的代码的当前状态是什么,至少上面的代码不能正常工作,所以我冒昧地重构了你的函数。我还从标记中删除了ID属性,并将其替换为class属性,因为您确实不应该克隆ID 以下版本似乎在FF中:
下次我为您做代码时,请正确缩进代码。不仅是为了我们这些寻求帮助的人,为了你自己,你才能够阅读自己的代码。你的克隆方法似乎会创建重复的元素ID。我不确定这是否是上述问题的根本原因,但无论如何,这是一种不好的做法。
var max_upload = 10;
var make_upload = function() {
var $uploads = $(".fileupload");
if ($uploads.length >= max_upload) {
alert("You can only upload up to 10 files");
} else {
var $last = $uploads.last();
$last
.clone()
.insertAfter($last)
.find("input")
.val("")
.bind("change", function() {
make_upload();
});
}
}
make_upload();