按下回车键时,尝试获取下拉建议以转到指定的网址。我试过创建一个可以读取地址的函数,但是我被卡住了!任何帮助表示赞赏!
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li><a href=" + item.address + ">" + item.category + "</a></li>" );
currentCategory = item.category;
} //that._renderItemData( ul, item );
});
}
});
var data=[
{ label: "96710", category: "Site 2", address: "http://www.test.com/site2" },
{ label: "96718", category: "Site 2", address: "http://www.test.com/site2" },
{ label: "96720", category: "Site 2", address: "http://www.test.com/site2" },
{ label: "96719", category: "Site 1", address: "http://www.test.com/site1" },
{ label: "96725", category: "Site 1", address: "http://www.test.com/site1" },
{ label: "96814", category: "Site 3", address: "http://www.test.com/site3" },
{ label: "96815", category: "Site 3", address: "http://www.test.com/site3" }
];
$(".auto").catcomplete({
source: data,
minLength: 1,
autofocus: true,
});
答案 0 :(得分:0)
打开控制台我在键入第一个字符时发现了问题。 请注意:
callback
$(document).ready(callback)
中,以确保在触发javascript / jQuery之前加载html,css。onkeypress="isNumberKey(event)"
,你应该用jQuery绑定事件。在修正上述两点之后,我有:https://jsfiddle.net/oey5vfg0/3/
我可以按label
搜索(例如:96790)。现在只需修复渲染部分。