简体   繁体   中英

Multiple Image upload with previw and delete , Jquery and Javascript

I have problem here. I have with this code multiple selected and preview image but I need to delete the selected image. For example, I select 5 images to preview and I have a preview but I didn't like one of them I want to delete the selected picture. Here I have just reset when I clicked to remove the image. With old code (I put in comment) I have just hidden image not deleted. I tried to find a solution already here but it's not working.

 $(document).ready(function() { if (window.File && window.FileList && window.FileReader) { $("#files").on("change", function(e) { var files = e.target.files, filesLength = files.length; for (var i = 0; i < filesLength; i++) { var f = files[i] var fileReader = new FileReader(); fileReader.onload = (function(e) { var file = e.target; $("<span class=\"pip\">" + "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" + "<br/><span class=\"remove\">Remove image</span>" + "</span>").insertAfter("#files"); $(".remove").click(function(){ $(this).parent(".pip").remove(); $('#files').val(""); }); /* $(".remove").click(function(){ $(this).parent(".pip").remove(); });*/ }); fileReader.readAsDataURL(f); } }); } else { alert("Your browser doesn't support to File API") } });
 input[type="file"] { display: block; }.imageThumb { max-height: 75px; border: 2px solid; padding: 1px; cursor: pointer; }.pip { display: inline-block; margin: 10px 10px 0 0; }.remove { display: block; background: #444; border: 1px solid black; color: white; text-align: center; cursor: pointer; }.remove:hover { background: white; color: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <div class="field" align="left"> <h3>Upload your images</h3> <input type="file" id="files" name="files[]" multiple /> </div>

First of all, add a single handler for the remove buttons outside the $("#files").on("change" , so handler's logic would run only once. In the new handler you have to find the index of the picture to remove, and then create a DataTransfer with the filtered files. In the end, overwrite input's files with DT's files.

$(document).on('click', '.remove', function(){
            var pips = $('.pip').toArray();
            var $selectedPip = $(this).parent('.pip');
            var index = pips.indexOf($selectedPip[0]);

            var dt = new DataTransfer();
            var files = $("#files")[0].files;

            for (var fileIdx = 0; fileIdx < files.length; fileIdx++) {
                if (fileIdx !== index) {
                dt.items.add(files[fileIdx]);
              }
            }

            $("#files")[0].files = dt.files;

            $selectedPip.remove();
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM