我使用jquery ui autocomplete作为多列自动完成功能。代码如下:
$(function (){
//value
var isi = [
{
jng: 'java',
wrn: 'write once run anywhere java',
tpe: 'bla',
kde: 'boh'
},
{
jng: 'PHP',
wrn: 'write once run anywhere PHP',
tpe: 'bli',
kde: 'bah'
}
];
//code
$('#name').autocomplete({
minLength: 1,
source: isi,
focus: function (event, ui){
$('#name').val(ui.item.jng);
return false;
},
select: function (event, ui){
$('#name').val(ui.item.kde);
return false;
}
})
.data('ui-autocomplete')._renderItem = function (ul, item){
return $('<li>')
.append("<a>" + item.jng + " " + item.wrn + " " + item.tpe + "</a>")
.appendTo(ul);
};
});
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<input id='name' type='text'/>
如果我输入“j”或空格,那么代码就可以工作了。当我输入另一封信时,它又消失了。 这段代码出了什么问题?
答案 0 :(得分:0)
$(function (){
//value
var isi = [
{
jng: 'java',
wrn: 'write once run anywhere java',
tpe: 'bla',
kde: 'boh'
},
{
jng: 'PHP',
wrn: 'write once run anywhere PHP',
tpe: 'bli',
kde: 'bah'
}
];
//code
$('#name').autocomplete({
minLength: 3,
source: isi,
focus: function (event, ui){
$('#name').val(ui.item.jng);
return false;
},
select: function (event, ui){
$('#name').val(ui.item.kde);
return false;
}
})
.data('ui-autocomplete')._renderItem = function (ul, item){
return $('<li>')
.append("<a>" + item.jng + " " + item.wrn + " " + item.tpe + "</a>")
.appendTo(ul);
};
});
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<input id='name' type='text'/>
的项目