我刚刚在我的网站中实现了一个Javascript自动填充搜索框,但我可以触发子页面链接的唯一方法是使用鼠标单击列表项。 我想要做的是,除了可以通过已经实现的键盘进行选择之外,还有可能回击"返回"键盘上的键,然后转到所选的登录页面。
我使用的是:
<script>
$(function() {
var projects = [
{
value: "value1",
label: "value1",
desc: "descr1",
icon: "icon1.png",
link: "http://url1"
},
{
value: "value2",
label: "value2",
desc: "descr2",
icon: "icon2.png",
link: "http://url2"
}
];
$( "#project" ).autocomplete({
minLength: 2,
source: projects,
focus: function( event, ui ) {
$( "#project" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
$( "#project-description" ).html( ui.item.desc );
$( "#project-icon" ).attr( "src", ui.item.icon );
return false;
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<a href='" + item.link + "'>" + "<img style='height:35px;' src='" + item.icon + "'/>" + " " + item.label + " - " + item.desc + "</a>" )
.appendTo( ul );
};
});
</script>
我使用的HTML是:
<input id="project"></div>
您可以在此处查看示例:http://www.lolgame.it/lolgame-counterpicks/
非常感谢大家
答案 0 :(得分:0)
我们走了。在此测试:http://jsfiddle.net/agWH2/
$( "#project" ).autocomplete({
minLength: 1,
source: projects,
focus: function( event, ui ) {
$( "#project" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
if (event.keyCode == 13) {
window.location.href = ui.item.link;
}
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
$( "#project-description" ).html( ui.item.desc );
$( "#project-icon" ).attr( "src", ui.item.icon );
return false;
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<a href='" + item.link + "'>" + "<img style='height:35px;' src='" + item.icon + "'/>" + " " + item.label + " - " + item.desc + "</a>" )
.appendTo( ul );
};