我正在尝试让jquery自动完成填充并在第一个字符后显示菜单,但我当前的代码不会这样做。它在输入第二个字符后有效,并且在删除输入的字符时也有效。想法?
$("#resource").keyup(function(){
var s = $("#resource").val();
$( "#resource" ).autocomplete({ minLength: 0 });
$.post("URL_TO_PHP_SCRIPT_JSON_DATA_RETURNED",
{search:s},
function(data,status){
if (status == 'success') {
var obj = JSON.parse(data);
var availableTags = [];
if (obj.length > 0) {
var tags = [];
for (var i = 0; i < obj.length; i++) {
var name = obj[i]['last_name']+', '+obj[i]['first_name'];
var u_id = obj[i]['user_id'];
availableTags.push( {value:name,label:name,rID:u_id} );
}
}
$("#resource").autocomplete({
minLength: 0,
source: availableTags,
focus: function( event, ui ) {
$("#resource").val( ui.item.label );
$("#rID").val( ui.item.rID );
return false;
},
select: function( event, ui ) {
$("#resource").val( ui.item.label );
$("#rID").val( ui.item.rID );
return false;
}
});
}
}
);
});