获取多个输入文件的图像预览

时间:2018-11-05 11:20:41

标签: javascript jquery html

我有两个<input type="file" />元素,对于这两个输入,我希望使用不同的图片预览。但是,我只能让它为一个输入工作。如何对多个输入文件执行此操作?

我希望你们中的一些人对我有一个很好的榜样,在此先感谢您。

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#image-1')
        .attr('src', e.target.result)
    };
    reader.readAsDataURL(input.files[0]);
  }
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form enctype="multipart/form-data" action="" method="POST">
  <div class="form-group row">
    <div class="col-4">
      <img id="image-1" style=" height: 100%;" class="form-control" src="./Images/smc.png" />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="bestand" name="userfile" type="file" onchange="readURL(this);" /><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control" type="text" name="newname" value="SMC.png"><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <img style=" height: 100%;" class="form-control" src="./Images/achtergrond.png" />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="bestand" name="userfile1" type="file" /><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control" type="text" name="newname1" value="achtergrond.png"><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control submit" id="bestand" type="submit" name="instellingen1" value="Bestand uploaden" />
    </div>
  </div>
</form>

1 个答案:

答案 0 :(得分:1)

问题是因为您仅从第一个输入呼叫readURL()。您需要从所有人中调用它。您还需要修改逻辑,使其基于HTML结构而不是id选择的特定元素。为此,您可以使用DOM遍历来查找与img相关的input,后者已选择了文件。具体而言,在这种情况下,closest()find()方法。试试这个:

$('input:file').on('change', function() {
  var input = this;
  var $input = $(input);
  
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {      
      $input.closest('.form-group').prev('.form-group').find('img').prop('src', e.target.result)
    };
    reader.readAsDataURL(input.files[0]);
  }
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<form enctype="multipart/form-data" action="" method="POST">
  <div class="form-group row">
    <div class="col-4">
      <img style=" height: 100%;" class="form-control" src="./Images/smc.png" />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="bestand" name="userfile" type="file" /><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control" type="text" name="newname" value="SMC.png"><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <img style=" height: 100%;" class="form-control" src="./Images/achtergrond.png" />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="bestand" name="userfile1" type="file" /><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control" type="text" name="newname1" value="achtergrond.png"><br />
    </div>
  </div>
  <div class="form-group row">
    <div class="col-4">
      <input class="form-control submit" id="bestand" type="submit" name="instellingen1" value="Bestand uploaden" />
    </div>
  </div>
</form>

此外,请注意,在代码中使用的是jQuery的两个版本,应避免使用。其中之一就是没有AJAX功能的“超薄”版本。这并不是天生的错误,只是需要注意的事情。