2017-02-27 51 views
0

我嘗試添加下一個圖像使用選擇2到文本,這裏是我的代碼:添加在旁邊以選擇2文本圖像發送對象

$(".js-example-templating").select2({ 
    templateResult: formatState, 
    templateSelection: formatState 
}); 
function formatState (state) { 
    if (!state.id) { return state.text; } 
    var $state = $(
    '<span><img src="/dist/images/icons/[email protected]" class="img-flag" /> ' + state.text + '</span>' 
); 
    return $state; 
}; 

我在選擇菜單中的[目標對象]: enter image description here 當我選擇我得到了我想要的用戶之一: enter image description here

我找不到我的錯誤!

+0

嘗試'的console.log(州)'這樣就可以調試一下,看看可取屬性使用。 – PacMan

+0

console.log($ state)顯示「[Object Object]」,但是當我使用谷歌瀏覽器斷點時,我可以看到$ state正確地填滿了我想要的範圍 –

+0

我不知道爲什麼,它適用於即使im爲templateResult和templateSelection調用相同的函數,也不是列表中的對象 –

回答

0

我終於找到了我的錯誤,我不得不jQuery對象轉換爲字符串,在發送前:

return $state.prop('outerHTML');