[英]How to preview images with many inputs?
How can i preview multiple images uploaded from different inputs?如何预览从不同输入上传的多个图像?
$(".img").change(function() { if (this.files && this.files[0]) { var img = this; var reader = new FileReader(); reader.onload = function(e) { $(img).closest('div.image_container').find('.imageLive').attr('src', e.target.result); }; reader.readAsDataURL(this.files[0]); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="image_container"> <div class="col-xs-2 one_card_center"> <input type="file" name="image" class="img" id="img"/> <label for="img" class="img_color">اضف الصورة</label> </div> <div class="col-xs-6"> <img src="{{ asset('cp/assets/images/placeholder.webp') }}" class="imageLive" id="imageLive"> </div> </div> <div class="image_container"> <div class="col-xs-2 one_card_center"> <input type="file" name="image" class="img" id="img"/> <label for="img" class="img_color">اضف الصورة</label> </div> <div class="col-xs-6"> <img src="{{ asset('cp/assets/images/placeholder.webp') }}" class="imageLive" id="imageLive"> </div> </div>
( I did when adding new input all things is good with this but need with image preview ) (我在添加新输入时所做的一切都很好,但需要图像预览)
I need when user add new input and add image preview it does not matter how many inputs add当用户添加新输入并添加图像预览时,我需要添加多少输入并不重要
You're attaching the image to every matching imageLive
element:您将图像附加到每个匹配的
imageLive
元素:
$('.imageLive').attr('src', e.target.result);
Presumably you only want the nearest imageLive
element.大概你只想要最近的
imageLive
元素。 Wrap your overall structure in a container of some sort:将您的整体结构包装在某种容器中:
<div class="image_container">
<div class="col-xs-2 one_card_center">
<input type="file" name="image" class="img" id="img" style="display:none;"/>
<label for="img" class="img_color">add image</label>
</div>
<div class="col-xs-6">
<img src="{{ asset('cp/assets/images/placeholder.webp') }}" class="imageLive" id="imageLive">
</div>
</div>
Then from the current .img
element you can traverse up to that container and back down to the target element:然后从当前的
.img
元素,您可以向上遍历到该容器并返回到目标元素:
$(this).closest('div.image_container').find('.imageLive').attr('src', e.target.result);
Edit: It looks like you'll also need to capture the reference to this
, because that context changes in the callback from the FileReader
.编辑:看起来您还需要捕获对
this
的引用,因为该上下文在FileReader
的回调中发生了变化。 You can capture it in a variable to use within the callback.您可以在一个变量中捕获它以在回调中使用。 For example:
例如:
$(".img").change(function() { if (this.files && this.files[0]) { var img = this; var reader = new FileReader(); reader.onload = function(e) { $(img).closest('div.image_container').find('.imageLive').attr('src', e.target.result); }; reader.readAsDataURL(this.files[0]); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="image_container"> <div class="col-xs-2 one_card_center"> <input type="file" name="image" class="img"/> <label for="img" class="img_color">اضف الصورة</label> </div> <div class="col-xs-6"> <img src="" class="imageLive"> </div> </div> <div class="image_container"> <div class="col-xs-2 one_card_center"> <input type="file" name="image" class="img"/> <label for="img" class="img_color">اضف الصورة</label> </div> <div class="col-xs-6"> <img src="" class="imageLive"> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.