0

如果我有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; 
} 

回答

0

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

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