Не работает preview изображения
let $block = $('.block-variants').clone();
$('.click_to_add_block').click(function() {
$(this).before($block.clone());
});
$(document).on('click', '.delete', function() {
$(this).parent().remove();
});
function readURL() {
var $input = $(this);
var $newinput = $(this).parent().parent().parent().find('.portimg ');
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
reset($newinput.next('.delbtn'), true);
$newinput.attr('src', e.target.result).show();
$newinput.after('<button type="button" class="delbtn removebtn">x</button>');
}
reader.readAsDataURL(this.files[0]);
}
}
$(".fileUpload").change(readURL);
$(".choose-file").on('click', '.delbtn', function(e) {
reset($(this));
});
function reset(elm, prserveFileName) {
if (elm && elm.length > 0) {
var $input = elm;
$input.prev('.portimg').attr('src', '').hide();
if (!prserveFileName) {
$('input.fileUpload').val("");
}
elm.remove();
}
}
img.portimg {
display: none;
max-width: 200px;
max-height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="block-variants">
<div class="choose-file">
<div class=socialmediaside2>
<input class=fileUpload name=profilepic[] type=file>
</div>
<div class=upload-demo>
<div class=upload-demo-wrap><img class=portimg src=#></div>
</div>
</div>
</div>
<button class="click_to_add_block" type="button">Добавить вариант</button>
При клонировании блока, а потом загрузке фото перестает работать preview - это первая проблема.
Вторая проблема заключается в том, что если делаю загрузку для нескольких файлов, отображается preview только для одной фото.
Помогите разобраться в этих двух проблемах.
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
$(document).on("change", ".fileUpload", readURL);
$(document).on('click', '.choose-file .delbtn', function (e) {
reset($(this));
});