格式中的图像在选择事件时将用户发送到新页面时,选择标记被破坏

Image in formatSelection markup is broken when sending user to new page on selecting event

本文关键字:选择 新页面 用户 图像 事件 格式      更新时间:2023-09-26

如果我有一个包含图像的formatSelection的标记,并且在select2-selecting事件中有window.location.href,则图像已损坏。如果我删除window.location.href,图像就可以工作,并且可以在加载新页面之前看到。

$('.select2#topbarSearch').on("select2-selecting", function(e) { 
    window.location.href = 'www.example.com';
});

function selectionFormat(data) {
        var markup = "<table class='search-result'><tr>";
        if (data.image !== undefined) {
            markup += "<td class='data-image'><img style='height: 25px;' src='" + data.image + "'/></td>";
        }
        markup += "<td class='data-info-selected'><div class='data-title'>" + data.title + "</div>";
        markup += "</td></tr></table>";
        return markup;
}

在重定向用户之前,必须验证图像是否已加载完毕。

var img = new Image();
img.src = e.object.image;
img.onload = function(){
    window.location.href = scriptPath + 'item/' + e.object.id;
};