在没有关闭模式的情况下更换后,在模态窗口中刷新图像

问题描述 投票:0回答:1

我尝试在更换后在模态窗口中刷新我的图像。如果我在不关闭模态窗口的情况下重复操作,我的<div>会显示所有连续的图片,如下所示:

jQuery代码:

$(document).ready(function(){
    $('#img_logo_content').change(function(){
        var fd = new FormData();
        var files = $('#img_logo_content')[0].files[0];
        fd.append('file',files);
        fd.append('request',1);

        // AJAX request
        $.ajax({
            url: 'upload.php',
            type: 'post',
            cache: false,
            data: fd,
            contentType: false,
            processData: false,
            success: function(response){
                if(response != 0){
                    // Show image preview
                     $('#logo-preview').append("<img src='"+response+"'  height='50' style='display: inline-block;'>");
                }else{
                    alert('file not uploaded');
                }
            }
        });
    });
});

我的模态div

<div class="form-group">
    <label for="img_logo_content" class="col-sm-4 control-label">Votre logo</label>
    <div class="col-sm-8">
        <!-- Display logo -->
        <div id="logo-preview"></div>
        <input type="file" class="form-control" id="img_logo_content" name="img_logo_content" placeholder="Votre Logo">
    </div>
</div>
php jquery bootstrap-modal
1个回答
0
投票

将.append()更改为.html()

$('#logo-preview').html("<img src='"+response+"'  height='50' style='display: inline-block;'>");

这应该替换图像而不是附加到该元素的末尾,但是,这将替换该元素中的所有html。不确定是否需要。

© www.soinside.com 2019 - 2024. All rights reserved.