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; 
}; 
相關問題