2017-01-20 39 views
0

我試圖讓我所有的預覽圖像對.remove()成功,並想知道我會如何做到這一點。多個文件上傳成功後刪除()圖像

發生了什麼 - 如果說我上傳的兩張圖片。只有兩個圖像中的一個將刪除()上的成功;如果我上傳三張圖片然後上傳圖片,三張圖片的兩張預覽仍然存在。所以它只能刪除一個預覽圖像。我相信這是最後插入的預覽圖像。

這是我所擁有的。

//functions on change event of file input to select different file 
    $('body').on('change', '#image_data', function(){ 
       if (this.files && this.files[0]) { 
        abc += 1; //increementing global variable by 1 

        var z = abc - 1; 
        var x = $(this).parent().find('#previewimg' + z).remove(); 
        $(this).before("<div id='abcd"+ abc +"' class='abcd'><img id='previewimg" + abc + "' src=''/></div>"); 

        var reader = new FileReader(); 
        reader.onload = imageIsLoaded; 
        reader.readAsDataURL(this.files[0]); 

        })); 
       } 
      }); 

//To preview image  
    function imageIsLoaded(e) { 
     $('#previewimg' + abc).attr('src', e.target.result); 
    }; 

成功

success: function(data) 
{ 
$("#previewimg" + abc).remove(); 
} 

回答

0

當你被IDS所有人都沒有得到一次在去除下來。 如果要一次刪除所有圖像,可以使用類而不是Ids。

//functions on change event of file input to select different file 
$('body').on('change', '#image_data', function(){ 
      if (this.files && this.files[0]) { 
       abc += 1; //increementing global variable by 1 

       var z = abc - 1; 
       var x = $(this).parent().find('#previewimg' + z).remove(); 
       $(this).before("<div id='abcd"+ abc +"' class='abcd'><img id='previewimg" + abc + "' src='' class='previewImages'/></div>"); 

       var reader = new FileReader(); 
       reader.onload = imageIsLoaded; 
       reader.readAsDataURL(this.files[0]); 

       })); 
      } 
     }); 

//To preview image  
    function imageIsLoaded(e) { 
     $('#previewimg' + abc).attr('src', e.target.result); 
}; 

在成功:

success: function(data) 
{ 
    $(".previewImages").remove(); 
} 
+0

絕對精彩。對我來說有詭計了。爲什麼我在這麼簡單的事情上絞盡腦汁呢。感謝您的幫助/非常感謝。 –

+0

:)太棒了!快樂它幫助! – poushy

+0

只要我能再次感謝,我一定會接受答案 –

相關問題