Image Upload with preview and Delete option - Javascript / Jquery

后端 未结 3 585
执念已碎
执念已碎 2020-12-07 13:15

I have code below that runs perfectly and uploads multiple images.

This is the html and jQuery code:

3条回答
  •  执念已碎
    2020-12-07 13:52

    Try adding this : .click(function(){$(this).remove();});. It will remove the image by clicking it.

    EDIT Improved version included.

    EDIT 2 Since people keep asking, please see this answer on manually deleting a file from a FileList (spoiler: it's not possible...).

    $(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;
              $("" +
                "" +
                "
    Remove image" + "
    ").insertAfter("#files"); $(".remove").click(function(){ $(this).parent(".pip").remove(); }); // Old code here /*$("", { class: "imageThumb", src: e.target.result, title: file.name + " | Click to remove" }).insertAfter("#files").click(function(){$(this).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;
    }
    
    

    Upload your images

提交回复
热议问题