2014-06-09 19 views
0

我成功地將預覽圖像從查看區域中移除,但實際上並沒有將它從陣列中刪除。我無法弄清楚如何刪除它,因爲它從未被選中,請幫助我!jquery從陣列中刪除對象使用html5進行圖像預覽

http://jsfiddle.net/ERZVC/

var count=0; 
function handleFileSelect(evt) { 
    var $fileUpload = $("input#files[type='file']"); 
    count=count+parseInt($fileUpload.get(0).files.length); 

    if (parseInt($fileUpload.get(0).files.length) > 6 || count>5) { 
     alert("You can only upload a maximum of 5 files"); 
     count=count-parseInt($fileUpload.get(0).files.length); 
     evt.preventDefault(); 
     evt.stopPropagation(); 
     return false; 
    } 
    var files = evt.target.files; 
    for (var i = 0, f; f = files[i]; i++) { 
     if (!f.type.match('image.*')) { 
      continue; 
     } 
     var reader = new FileReader(); 

     reader.onload = (function (theFile) { 
      return function (e) { 
       var span = document.createElement('span'); 
       span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', escape(theFile.name), '"/><span class="remove_img_preview"></span>'].join(''); 
       document.getElementById('list').insertBefore(span, null); 
      }; 
     })(f); 

     reader.readAsDataURL(f); 
    } 
} 

$('#files').change(function(evt){ 
    handleFileSelect(evt); 
}); 

$('#list').on('click', '.remove_img_preview',function() { 
    $(this).parent('span').remove(); 
}); 

HTML

<input type="file" id="files" name="image_file_arr[]" multiple> 
<br><output id="list"></output> 
+0

可能重複http://stackoverflow.com/questions/24470379/clear-an-image-preview-file-that-was - 從數組中刪除jquery) – tftd

回答

0

這裏被更新fiddle

唯一的錯誤是你沒有減少計數器在刪除文件。

謝謝。

var count=0; 
function handleFileSelect(evt) { 
    var $fileUpload = $("input#files[type='file']"); 
    count=count+parseInt($fileUpload.get(0).files.length); 

    if (parseInt($fileUpload.get(0).files.length) > 6 || count>5) { 
     alert("You can only upload a maximum of 5 files"); 
     count=count-parseInt($fileUpload.get(0).files.length); 
     evt.preventDefault(); 
     evt.stopPropagation(); 
     return false; 
    } 
    var files = evt.target.files; 
    for (var i = 0, f; f = files[i]; i++) { 
     if (!f.type.match('image.*')) { 
      continue; 
     } 
     var reader = new FileReader(); 

     reader.onload = (function (theFile) { 
      return function (e) { 
       var span = document.createElement('span'); 
       span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', escape(theFile.name), '"/><span class="remove_img_preview"></span>'].join(''); 
       document.getElementById('list').insertBefore(span, null); 
      }; 
     })(f); 

     reader.readAsDataURL(f); 
    } 
} 

$('#files').change(function(evt){ 
    handleFileSelect(evt); 
}); 

$('#list').on('click', '.remove_img_preview',function() { 
    $(this).parent('span').remove(); 
    count--; 
}); 
[即是從與jquery數組中刪除清除圖像預覽文件(的
+0

不錯!這適用於preivew,現在如果我上傳4張圖像然後刪除2,在提交時只會上傳預覽中的2張圖片? – Gadgetster

+0

爲此,您必須使用JavaScript的拼接方法在刪除按鈕單擊時從文件數組中刪除該文件。這裏是參考鏈接:http://www.w3schools.com/jsref/jsref_splice.asp。你也可以使用alert(JSON.stringify(files))查看你的文件數組。把這一行放在var files = evt.target.files;在你的代碼中,你可以看到上傳的文件列表。謝謝 –

+0

你能告訴我如何將這兩個代碼放在代碼中,這樣在它看到文件後,它將刪除那些被點擊刪除的文件? – Gadgetster