如何在图像预览中多次显示文件输入按钮?

时间:2018-09-06 13:21:05

标签: javascript php jquery

我正在使用laravel 5.6,并且我的表单具有图像上传按钮。当前它正在使用带有图像缩略图预览的上传单个文件。

<h3>Upload  images</h3>
<input type="file" id="files" name="files[]" multiple />

jquery

<script >
    $(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();
                        });
                    });
                    fileReader.readAsDataURL(f);
                }
            });
        } else {
            alert("Your browser doesn't support to File API")
        }
    });
</script>

css

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;
}

现在,我将另一个上传输入添加到相同的表单中,

<div class="form-group row required">
    <div class="field" align="left" >
        <h3>Upload  images</h3>
        <input type="file" id="files" name="files[]" multiple />
        <input type="file" id="files" name="files[]" multiple />//new one
    </div>
</div>

然后,第一个上传按钮可以显示图像缩略图,但是第二个文件输入不显示缩略图。问题是什么?如何解决此问题?

1 个答案:

答案 0 :(得分:1)

不要给两个按钮相同的id(您提供的任何ID必须唯一),而是给它们相同的class,然后您就可以在Javascript中引用它们了。

<div class="form-group row required">
    <div class="field" align="left" >
        <h3>Upload  images</h3>
        <input type="file" class="files" name="files[]" multiple />
        <input type="file" class="files" name="files[]" multiple />//new one
    </div>
</div>

然后,您需要更新Javascript以基于类而不是ID绑定侦听器。 .insertAfter()也会更新,以引用所单击的特定项目。

$(document).ready(function() {
  if (window.File && window.FileList && window.FileReader) {
    $(".files").on("change", function(e) {  // THIS LINE CHANGED
        var clickedButton = this;  // THIS LINE IS NEW
        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(clickedButton);  // THIS LINE CHANGED

            $(".remove").click(function(){
              $(this).parent(".pip").remove();
            });
          });

          fileReader.readAsDataURL(f);
        }
    });
  } else {
    alert("Your browser doesn't support to File API")
  }
});

DEMO