我有以下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);
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>
答案 0 :(得分:0)
我不知道代码的当前状态是什么(上面的代码至少不能起作用),所以我冒昧地重构你的函数。我还从标记中删除了ID属性,并将其替换为class属性,因为您确实不应该克隆ID。
以下版本在FF中似乎work just fine:
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();