图像属性格式不正确

Image attribute not properly formatted

本文关键字:不正确 格式 属性 图像      更新时间:2023-09-26

>我正在尝试将图像属性附加到我的div图像容器中,但如我所见,图像不在同一个容器中

<img id="imgUpload" style="width: 140px; height: 140px;" class="img-thumbnail"></img>
<img src="static/uploads/4178ba43-a9f1-4315-b8ea-88c0531cc042.png"><img id="imgUpload" style="width: 140px; height: 140px;" class="img-thumbnail"></img>

下面是以下脚本。

<script>
    $(function(){
        $('#fileupload').fileupload({
            url: 'upload',
            dataType: 'json',
            add: function (e, data) {
                data.submit();
            },
            success:function(response,status) {
                console.log(response.filename);
                var filePath = 'static/uploads/' + response.filename;
                $("#imgUpload").append($("<img>", { src: filePath }));
                $('#filePath').val(filePath);
                console.log('success');
            },
            error:function(error){
                console.log(error);
            }
        });
    })
</script>

以下是应该扮演该角色的 html 元素。

<div class="pull-right">
    <img id="imgUpload" style="width: 140px; height: 140px;" class="img-thumbnail"/><input type="hidden" name="filePath" id="filePath"></input>
</div>
</div>

更改以下行

$("#imgUpload").append($("<img>", { src: filePath }));

$("#imgUpload").attr("src", filePath);

我看了你之前的问题

我认为你需要这个

var img = $('<img />', { 
  src: filePath
});
img.appendTo($('.pull-right'));

你这样使用 Attr $("#imgUpload").append($("<img>", { src: filePath }));$("#imgUpload").attr("src", filePath);

此示例

<script>
$(function(){
    $('#fileupload').fileupload({
        url: 'upload',
        dataType: 'json',
        add: function (e, data) {
            data.submit();
        },
        success:function(response,status) {
            console.log(response.filename);
            var filePath = 'static/uploads/' + response.filename;
            $("#imgUpload").attr("src", filePath);
            $('#filePath').val(filePath);
            console.log('success');
        },
        error:function(error){
            console.log(error);
        }
    });
})