У меня ниже jquery, который показывает предварительный просмотр изображения из ввода файла.
$(".imgshowinput").change(function () {
readURL(this);
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.imgshow').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
Приведенный выше код работает хорошо, но проблема обновляет все элементы img, используя один и тот же класс.
лезвие
<div class = "card" style = "width: 18rem;">
<img id = "studphoto" name = "studphoto" class = "card-img-top imgshow" src = "" alt = "Card image cap">
<div class = "card-body">
<input type = "file" name = "spic" class = "imgshowinput" accept = "image/*">
</div>
</div>
<div class = "card" style = "width: 18rem;">
<img id = "aadphoto" name = "aadphoto" class = "card-img-top imgshow" src = "" alt = "Card image cap">
<div class = "card-body">
<input type = "file" name = "aadpic" class = "imgshowinput" accept = "image/*">
</div>
</div>
Любое предложение обновить только определенный элемент img при использовании того же класса?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Просто добавьте идентификатор img, который вы хотите изменить, чтобы функция была:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#studphoto.imgshow').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
Используйте закрытый метод jquery, как показано ниже.
$(input).closest('.imgshow'). attr('src', e.target.result);
и полный javascript
$(".imgshowinput").change(function () {
readURL(this);
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(input).closest('.imgshow'). attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
Просто обновите свою функцию readURL следующим образом:
function readURL(input) {
// Gets .card for this input
var card = $(input).parent().parent();
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
// Finds img inside of .card and sets the attribute
$(card.find('img').attr('src', e.target.result));
}
reader.readAsDataURL(input.files[0]);
}
}
Я решил это для вас на моей странице кодовая ручка.
$(".imgshowinput").change(function () {
readURL(this);
});
function readURL(input) {
var card = $(input).parent().parent();
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(card.find('img').attr('src', e.target.result));
}
reader.readAsDataURL(input.files[0]);
}
}body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
color: #fcbe24;
background-color: #18222d;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.test {
display: flex;
}
img {
width: auto;
height: 200px;
}<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "card" style = "width: 18rem;">
<img id = "studphoto" name = "studphoto" class = "card-img-top imgshow" src = "" alt = "Card image cap">
<div class = "card-body">
<input type = "file" name = "spic" class = "imgshowinput" accept = "image/*">
</div>
</div>
<div class = "card" style = "width: 18rem;">
<img id = "aadphoto" name = "aadphoto" class = "card-img-top imgshow" src = "" alt = "Card image cap">
<div class = "card-body">
<input type = "file" name = "aadpic" class = "imgshowinput" accept = "image/*">
</div>
</div>