gpt4 book ai didi

javascript - 尽管具有不同的 id,但两个不同文件输入表单的图像预览出现在同一 div 中

转载 作者:行者123 更新时间:2023-11-28 14:15:40 31 4
gpt4 key购买 nike

我又遇到了一个小问题。我知道这不是一个最优雅的问题,但我被困住了,需要真正的帮助。

我正在尝试显示在文件输入表单中选择的文件的图像预览。我有一个小的 jQuery 片段,可以读取文件并预览它们。该代码对于一个文件输入运行良好,但我决定在其下方添加另一个文件输入,并复制粘贴 jQuery 脚本,更改第二个输入的文件输入 ID 和预览 div ID。但是两个文件输入的预览似乎出现在同一个 div 中。我对此很陌生,所以我很难调试。帮我解决这个问题。

$(document).ready(function(){
bsCustomFileInput.init();
function previewImages() {

var $preview = $('#preview').empty();
if (this.files) $.each(this.files, readAndPreview);

function readAndPreview(i, file) {

if (!/\.(jpe?g|png|gif)$/i.test(file.name)){
return alert(file.name +" is not an image");
} // else...

var reader = new FileReader();

$(reader).on("load", function() {
$preview.append($("<img/>", {src:this.result, height:100}));
});

reader.readAsDataURL(file);

}

}

$('#inputGroupFile').on("change", previewImages);

function previewImages() {

var $preview = $('#preview2').empty();
if (this.files) $.each(this.files, readAndPreview);

function readAndPreview(i, file) {

if (!/\.(jpe?g|png|gif)$/i.test(file.name)){
return alert(file.name +" is not an image");
} // else...

var reader = new FileReader();

$(reader).on("load", function() {
$preview.append($("<img/>", {src:this.result, height:100}));
});

reader.readAsDataURL(file);

}

}

$('#inputGroupFile2').on("change", previewImages);
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<title>Hello, world!</title>
</head>
<body>
<div class="container py-4">
<div class="row">
<div class="col-sm-12 mx-auto">

<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile" multiple>
<label class="custom-file-label" for="inputGroupFile" aria-describedby="inputGroupFileAddon">Choose image</label>
</div>
<div class="input-group-append">
<span class="input-group-text" id="inputGroupFileAddon">Upload</span>
</div>
</div>

<div id="preview"></div>

</div>
</div>
<div class="row">
<div class="col-sm-12 mx-auto">

<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile2" multiple>
<label class="custom-file-label" for="inputGroupFile2" aria-describedby="inputGroupFileAddon">Choose image</label>
</div>
<div class="input-group-append">
<span class="input-group-text" id="inputGroupFileAddon">Upload</span>
</div>
</div>

<div id="preview2"></div>

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

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bs-custom-file-input/dist/bs-custom-file-input.min.js"></script>
<script src="/images.js"></script>
</body>
</html>

最佳答案

您有重复的 ID:inputGroupFileAddon。

您有重复的函数。

您的主要问题是如何引用预览 div:

 $('#preview')

您可以将其更改为:

$(this).closest('.row').find('[id^="preview"]')

...向上直到行祖先并找到 id 以 preview 开头的元素

这样你就不再需要两个不同的函数了。

$('#inputGroupFile, #inputGroupFile2').on("change", function(e) {
var $preview = $(this).closest('.row').find('[id^="preview"]').empty();
if (this.files) $.each(this.files, readAndPreview);
function readAndPreview(i, file) {
if (!/\.(jpe?g|png|gif)$/i.test(file.name)){
return alert(file.name +" is not an image");
} // else...
var reader = new FileReader();
$(reader).on("load", function() {
$preview.append($("<img/>", {src:this.result, height:100}));
});
reader.readAsDataURL(file);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<div class="container py-4">
<div class="row">
<div class="col-sm-12 mx-auto">

<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile" multiple>
<label class="custom-file-label" for="inputGroupFile" aria-describedby="inputGroupFileAddon">Choose image</label>
</div>
<div class="input-group-append">
<span class="input-group-text" id="inputGroupFileAddon1">Upload</span>
</div>
</div>

<div id="preview"></div>

</div>
</div>
<div class="row">
<div class="col-sm-12 mx-auto">

<div class="input-group mb-3">
<div class="custom-file">
<input type="file" class="custom-file-input" id="inputGroupFile2" multiple>
<label class="custom-file-label" for="inputGroupFile2" aria-describedby="inputGroupFileAddon">Choose image</label>
</div>
<div class="input-group-append">
<span class="input-group-text" id="inputGroupFileAddon2">Upload</span>
</div>
</div>

<div id="preview2"></div>

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

关于javascript - 尽管具有不同的 id,但两个不同文件输入表单的图像预览出现在同一 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598703/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com