在我预览图像并提交后,该图像仍然保留在那里,并添加了消息评论。我希望图像预览在提交后自动删除。
这是我的 html 代码:
function showPreview(objFileInput) {
if (objFileInput.files && objFileInput.files[0]) {
var fileReader = new FileReader();
fileReader.onload = function (e) {
$("#targetLayer").html('<img src="'+e.target.result+'" width="200px" height="200px" class="upload-preview" />');
$("#targetLayer").css('opacity','0.7');
$(".icon-choose-image").css('opacity','0.5');
}
fileReader.readAsDataURL(objFileInput.files[0]);
}
}
$("#image_name").change(function(e) {
showPreview(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group" id="targetOuter">
<div id="targetLayer"></div>
<img src="photo.png" class="icon-choose-image" />
<div class="icon-choose-image">
<input name="image_name" id="image_name" type="file" class="inputFile" />
</div>
</div>
最佳答案
//Target the image element and remove
$(.icon-choose-image).remove();
关于javascript - 图片提交后如何取消预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56673429/