预览在动态添加/删除文件字段中未正确更新

时间:2018-05-07 08:15:51

标签: javascript

我创建了以下代码。除了图像预览之外,整个代码都正常工作。第一个字段正确更新图像预览。

但是当我们在后续动态文件字段中添加图像时,它仅更新第一个字段的预览。

我想更新每个文件字段的预览,并分别选择自己的图像...

以下是我的代码......

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="panel panel-default">
  <div class="panel-heading">
    <center><b>Team Members</b></center>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-md-4">
        <div class="thumbnail">
          <script type='text/javascript'>
            function preview_member(event) {
              var reader = new FileReader();

              reader.onload = function() {
                var output = document.getElementById('member_image');
                output.src = reader.result;
              }

              reader.readAsDataURL(event.target.files[0]);
            }
          </script>
          <img src="<?php echo esc_url( site_url('/profile-images/blank-image.png') ); ?>" id="member_image" alt="">
        </div>
        <div class="form-group">
          <label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event)" style="display: none;"></label>
        </div>
      </div>
      <div class="col-md-8">
        <div class="form-group">
          <label for="member_name">Member Name <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_name[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_role[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_email">Email address <b style="color:#FF0000;">*</b></label>
          <input type="email" class="form-control" name="member_email[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_facebook_id[]" placeholder="">
        </div>
      </div>
    </div>
    <button type="button" class="btn btn-success btn-block" id="add-member-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Member</button>
    <br>
    <div id="member-fields">

    </div>
  </div>
</div>

<script type="text/javascript">
  jQuery(document).ready(function($) {

    //fadeout selected item and remove
    $(document).on('click', '#remove-member-fields', function(event) {
      event.preventDefault();
      $(this).parent().fadeOut(300, function() {
        $(this).empty();
        return false;
      });
    });

    var rows = `<div class="team-member-fields"><div class="row"><div class="col-md-4"><div class="thumbnail"><img src="<?php echo esc_url( site_url(' / profile - images / blank - image.png ') ); ?>" alt=""></div><div class="form-group"><label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event)" style="display: none;"></label></div></div><div class="col-md-8"><div class="form-group"><label for="member_name">Member Name <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_name[]" placeholder=""></div><div class="form-group"><label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_role[]" placeholder=""></div><div class="form-group"><label for="member_email">Email address <b style="color:#FF0000;">*</b></label><input type="email" class="form-control" name="member_email[]" placeholder=""></div><div class="form-group"><label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_facebook_id[]" placeholder=""></div></div></div><button type="button" class="btn btn-danger btn-block" id="remove-member-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove Member</button><br></div></div></div>`;

    //add input
    $('#add-member-fields').click(function() {
      $(rows).fadeIn("slow").appendTo('#member-fields');
      i++;
      return false;
    });
  });
</script>

请帮我解决问题......谢谢...

1 个答案:

答案 0 :(得分:1)

您必须为每个图片代码添加特定ID。改变这些:

1 - 将您的row变量移至onclick事件

每次添加元素

后,

增加i个计数器

//add input
$("#add-member-fields").click(function() {
i++;

var rows = `<div class="team-member-fields"><div class="row"><div class="col-md-4"><div class="thumbnail"><img src="<?php echo esc_url( site_url(' / profile - images / blank - image.png ') ); ?>"
id="member_image${i}" alt=""></div><div class="form-group"><label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event, ${i})" style="display: none;"></label></div></div><div class="col-md-8"><div class="form-group"><label for="member_name">Member Name <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_name[]" placeholder=""></div><div class="form-group"><label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_role[]" placeholder=""></div><div class="form-group"><label for="member_email">Email address <b style="color:#FF0000;">*</b></label><input type="email" class="form-control" name="member_email[]" placeholder=""></div><div class="form-group"><label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_facebook_id[]" placeholder=""></div></div></div><button type="button" class="btn btn-danger btn-block" id="remove-member-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove Member</button><br></div></div></div>`;

$(rows)
  .fadeIn("slow")
  .appendTo("#member-fields");
return false;
});
});

3 - 更改功能输入参数并为其添加索引编号

function preview_member(event, inp) {
  var reader = new FileReader();
  console.log(inp);
  reader.onload = function() {
    var output = document.getElementById("member_image" + inp);
    output.src = reader.result;
  };

  reader.readAsDataURL(event.target.files[0]);
}

最终代码将是此

&#13;
&#13;
var i = 0;

function preview_member(event, inp) {
  var reader = new FileReader();
  console.log(inp);
  reader.onload = function() {
    var output = document.getElementById("member_image" + inp);
    output.src = reader.result;
  };

  reader.readAsDataURL(event.target.files[0]);
}

jQuery(document).ready(function($) {
  //fadeout selected item and remove
  $(document).on("click", "#remove-member-fields", function(event) {
    event.preventDefault();
    $(this)
      .parent()
      .fadeOut(300, function() {
        $(this).empty();
        return false;
      });
  });

  //add input
  $("#add-member-fields").click(function() {
    i++;

    var rows = `<div class="team-member-fields"><div class="row"><div class="col-md-4"><div class="thumbnail"><img src="<?php echo esc_url( site_url(' / profile - images / blank - image.png ') ); ?>"
id="member_image${i}" alt=""></div><div class="form-group"><label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event, ${i})" style="display: none;"></label></div></div><div class="col-md-8"><div class="form-group"><label for="member_name">Member Name <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_name[]" placeholder=""></div><div class="form-group"><label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_role[]" placeholder=""></div><div class="form-group"><label for="member_email">Email address <b style="color:#FF0000;">*</b></label><input type="email" class="form-control" name="member_email[]" placeholder=""></div><div class="form-group"><label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label><input type="text" class="form-control" name="member_facebook_id[]" placeholder=""></div></div></div><button type="button" class="btn btn-danger btn-block" id="remove-member-fields"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Remove Member</button><br></div></div></div>`;

    $(rows)
      .fadeIn("slow")
      .appendTo("#member-fields");
    return false;
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<div class="panel panel-default">
  <div class="panel-heading">
    <center><b>Team Members</b></center>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-md-4">
        <div class="thumbnail">
          <img src="<?php echo esc_url( site_url('/profile-images/blank-image.png') ); ?>" id="member_image0" alt="">
        </div>
        <div class="form-group">
          <label class="btn btn-success btn-block btn-file">Add Image<input type="file" name="member_image[]" onchange="preview_member(event, 0)" style="display: none;"></label>
        </div>
      </div>
      <div class="col-md-8">
        <div class="form-group">
          <label for="member_name">Member Name <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_name[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_role">Role in Project <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_role[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_email">Email address <b style="color:#FF0000;">*</b></label>
          <input type="email" class="form-control" name="member_email[]" placeholder="">
        </div>
        <div class="form-group">
          <label for="member_facebook_id">Facebook Username <b style="color:#FF0000;">*</b></label>
          <input type="text" class="form-control" name="member_facebook_id[]" placeholder="">
        </div>
      </div>
    </div>
    <button type="button" class="btn btn-success btn-block" id="add-member-fields"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Member</button>
    <br>
    <div id="member-fields">

    </div>
  </div>
</div>
&#13;
&#13;
&#13;