如果你在談論this plugin,下面應該工作:
假設你的JSON結果看起來是這樣的:
[
{
name: 'Google',
image: 'http://google.com/logo.png',
href: 'http://google.com'
},
{
name: 'Bing',
image: 'http://bing.com/logo.png',
href: 'http://bing.com/'
}
...
]
你需要在自己的自定義解析函數來傳遞你的內選項。此功能需要返回格式的對象數組:{ data: object, value: string, result: string }
$('#myfield').autocomplete('/search', {
parse: function(data) {
return $.map(data, function(item) {
return { data: item, value: item.name, result: item.href };
});
},
formatItem: function(item) {
return '<img src="' + item.image + '"/> ' + item.name;
}
})
.result(function(event, item) {
location.href = item.href;
});
有可能是一個更好的方式做鏈接,我知道我已經看到了其他自動完成/建議類型的插件,允許你這樣做這更容易,但我不記得哪些。希望這可以幫助。