如果您在谈论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;
});
可能有更好的方法来建立链接,我知道我见过其他自动完成/建议类型的插件可以让你更轻松地做到这一点,但我不记得是哪些。希望这会有所帮助。