Javascript JQuery UI自动完成-当用户单击文本框时打开菜单
我在用这个Javascript JQuery UI自动完成-当用户单击文本框时打开菜单,javascript,jquery,jquery-ui,autocomplete,jquery-autocomplete,Javascript,Jquery,Jquery Ui,Autocomplete,Jquery Autocomplete,我在用这个 当用户单击文本框时,如何使其自动打开菜单?我希望用户看到所有选项。您需要手动触发搜索事件,并将小部件上的minLength选项设置为零: $("input").autocomplete({ minLength: 0, /* other options */ }).on("focus", function () { $(this).autocomplete("search", ""); }); 工作示例:我想我真的明白了。如果将minLength设置为0,然后
当用户单击文本框时,如何使其自动打开菜单?我希望用户看到所有选项。您需要手动触发
搜索事件,并将小部件上的minLength
选项设置为零:
$("input").autocomplete({
minLength: 0,
/* other options */
}).on("focus", function () {
$(this).autocomplete("search", "");
});
工作示例:我想我真的明白了。如果将minLength设置为0,然后触发对“”的搜索,则会打开菜单
$(inputSelector).autocomplete(
{
source: this.validConstructCodes,
minLength: 0,
autoFocus: true,
autoSelect: true
});
$(inputSelector).focus(function(event) {
$(this).autocomplete( "search" , "" );
});
正如Andrew所解释的,你需要触发事件
但是,一旦从ajax请求中获得结果,最好再次显示结果,而不是再次询问服务器。minLength值是独立的,根据服务器请求的建议可以为2
$("input").autocomplete({
minLength: 2,
/* your options */
}).on("focus", function () {
/* the element with the search results */
var uid = $("#ui-id-"+$(this).autocomplete("instance").uuid);
if(uid.html().length == 0) {
/* same as $(this).autocomplete("search", this.value); */
$(this).keydown();
}
else {
uid.show();
}
});
使用jQuery Autocomplete v1.8对@amalesh answer稍作修改。
我向uuid添加了+1,因为它从0开始,但id设置为1
$("input").autocomplete({
minLength: 2,
/* your options */
}).on("focus", function () {
/* the element with the search results */
//
let uid = $("#ui-id-" + ($(this).autocomplete("instance").uuid + 1));
if(uid.html().length === 0) {
/* same as $(this).autocomplete("search", this.value); */
$(this).keydown();
}
else {
uid.show();
}
});
但是,在我们使用的版本中,我无法使其工作,将“on”替换为:。focus(function(){$(this).autocomplete(“search”,”)});很好。您可能正在使用早期版本的jQuery。不调用search
事件也可以吗?@soham.m17:我不这么认为。你想实现什么?实际上,整个列表在创建的ul
上可用,我想使用可用数据,不想刷新它。这将在UUID的末尾添加一个1。我不知道那会有什么效果。在autocomplete源代码中,您可以看到id的分配类似于#UIID+(++uuid),其中uuid为0。所以它在赋值之前增加一。如果只取这个值,那么它将查找#ui-id-0,至少在我的例子中,它是找不到的。