我想使用jQuery來複制我的文件輸入組,以便當用戶選擇一個文件上傳時,它會自動創建另一個文件上傳字段和說明字段。jQuery:重複文件輸入組
這工作正常,除了當我更改文件的文件,我已經選擇了一個輸入。當我這樣做時,它會複製所有文件輸入組。
任何洞察爲什麼這是怎麼回事?
的jsfiddle:http://jsfiddle.net/czLmbjd6/4/
HTML:
<div id = "add-photos">
<input type="file"></input>
<label>Description:</label>
<input type = "text"></input>
</div>
<div id = "additional-photos">
</div>
JS:
$(document).ready(function(){
bindFileInputs();
});
function bindFileInputs(){
$("input:file").change(function(){
addPhotoField();
});
}
function addPhotoField() {
var id = 1; //standin for now, will dynamically update this number later
createPhotoField(id);
}
function createPhotoField(id){
var p = $("#add-photos");
p.clone().appendTo("#additional-photos").removeAttr("id").children().find('input,select').each(function(){
$(this).val('');
if ($(this).attr('type') == 'file'){
console.log("type is file");
$(this).attr('name', 'data[Image][' + id + '][image]');
}
if ($(this).attr('type') == 'text'){
console.log("type is text");
$(this).attr('name', 'data[Image][' + id + '][description]');
}
});
bindFileInputs();
}
HM-仍具有它當您更改輸入選擇的文件創建太多的領域相同的錯誤。 – schnauss 2014-10-07 01:52:05