输入 jQuery 自动完成文本框后显示整个列表
我正在使用 jQuery 自动完成插件。有没有一种方法可以让用户输入文本框(已连接以自动完成)时,列表中会显示最上面的字母项目?某种触发器?
jQuery 代码
$('.someTextbox').autocomplete({
source: function (request, response) {
$.ajax({
url: serviceUrl + "/AddDocumentLinesService.svc/GetLineTypes",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
data: {
maxRows: 10,
textStartsWith: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.LineTypeCode + ' - (' + item.Description + ')',
value: item.LineTypeCode
}
}));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
});
我希望用户在输入 '$('.someTextbox')' 文本框时,列表就会出现。
I am using the jQuery autocomplete plugin. Is there a way that when a user enters a textbox (that is wired up to have autocomplete) the list appears with the top alphabetical items? Some sort of trigger?
jQuery Code
$('.someTextbox').autocomplete({
source: function (request, response) {
$.ajax({
url: serviceUrl + "/AddDocumentLinesService.svc/GetLineTypes",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
data: {
maxRows: 10,
textStartsWith: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.LineTypeCode + ' - (' + item.Description + ')',
value: item.LineTypeCode
}
}));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
});
I would like it so that the second the user enters the '$('.someTextbox')' textbox the list appears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
根据文档,.search() 手动触发搜索。您还可以将
options.minChars
设置为 0,并将options.delay
设置为较小的值,这也应该有效。请参阅:http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptionsTry:
According to the documentation, .search() triggers the search manually. You can also set
options.minChars
to 0 and theoptions.delay
to something small, which should also work. See: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions也许您可以尝试将
minChars
选项 设置为 0 ,并让GetLineTypes
检查给定字符串的长度。如果它的长度为 0,则返回第一个字母顺序的项目。另一种选择是连接到文本框的焦点事件并手动触发自动完成功能。也许你可以用这样的东西“欺骗”自动完成:
maybe you can try setting the
minChars
option to 0, and haveGetLineTypes
check for the length of the given string. if it's 0-length- return first alphabetical items.another option is to wire to the focus event of the textbox and to trigger the autocomplete functionality manually. maybe you can 'trick' the autocomplete with something like this:
所有答案都是正确的。我添加这个是因为 minChars 在新版本中已被弃用。
使用 minLength。
放
最小长度:0
您也可以考虑使用文本字段旁边的按钮,其作用类似于下拉菜单并切换所有可用选项。
快乐编码!
编辑
另外,由于您正在加载整个列表,因此您可以将延迟设置为 0,从而覆盖您可能提到的现有延迟行为。
All the answers are right.I'm adding this since minChars is deprecated in newer versions.
Use minLength.
Set
minLength : 0
Also you might consider using a button next to the textfield which kind of acts like a drop down and toggle for all available options.
Happy Coding!
Edit
Also since you are loading the whole list, you can make delay as 0,overriding existing behavior of delay which you might have mentioned.