2013-01-10 53 views
1

如何我使用jQuery自動完成加載在文本框中的值的圖像添加到文本框自動完成源

@Html.TextBox("Cities", "", new { @id="tags" }) 

<script type="text/javascript"> 
$(function() { 
     var availableTags = [ 
     "New York", 
     "London", 
     "Moscow", 
     "Paris", 
     "Berlin", 
     "Madrid", 
    ]; 
    $("#tags").autocomplete({ 
     source: availableTags 
    }); 
}); 

其實,我有超過1000個城市,我要去把它們存放在數據庫並通過模型發送到視圖。此外,獲得更愉快的外觀我想國家標誌添加到我的文本框狀的下拉列表:

enter image description here

我該怎麼辦呢?如何將圖像添加到自動編碼源?

回答

2

我重寫_renderMenu功能在我的解決方案:

_renderMenu: function(ul, items) { 
    var that = this; 
    $.each(items, function(index, item) { 
     that._renderItemData(ul, item) 
      .children("a") 
      .attr("title", item.value["@tooltip"]) 
      .prepend("<img class='menu_button_img' src='" + item.value["@icon"] + "' /> "); 
    }); 
}, 

爲我工作確定:)

+0

非常感謝。我試圖覆蓋_RenderItem函數,它的工作原理!請看看[ljsfiddle.net](http://jsfiddle.net/RsTvq/)我還有一個問題:如何選擇下拉菜單項並在靠近sity的文本框中顯示國旗? – mbigun

+0

以前從未做過。也許外部輸入圖像更好:看http://jqueryui.com/autocomplete/#custom-data – Saram

+1

這比我想象的更容易。剛添加文本框背景圖像的項目選擇事件[UPDATED jsfiddle.net](http://jsfiddle.net/RsTvq/1/) – mbigun