输入 jQuery 自动完成文本框后显示整个列表

发布于 2024-11-07 03:48:16 字数 1182 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反差帅 2024-11-14 03:48:16

尝试:

 $('.someTextbox').focus(function() { $(this).search(); });

根据文档,.search() 手动触发搜索。您还可以将 options.minChars 设置为 0,并将 options.delay 设置为较小的值,这也应该有效。请参阅:http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

Try:

 $('.someTextbox').focus(function() { $(this).search(); });

According to the documentation, .search() triggers the search manually. You can also set options.minChars to 0 and the options.delay to something small, which should also work. See: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

甜嗑 2024-11-14 03:48:16

也许您可以尝试将 minChars 选项 设置为 0 ,并让 GetLineTypes 检查给定字符串的长度。如果它的长度为 0,则返回第一个字母顺序的项目。

另一种选择是连接到文本框的焦点事件并手动触发自动完成功能。也许你可以用这样的东西“欺骗”自动完成:

$('.sometextbox').focus(function() {
   if ($('.sometextbox').val().length ==0) { 
   //no text entered yet
   $('.sometextbox').val('  '); //insert 2 white spaces in order to trigger the autocomplete
}
});

maybe you can try setting the minChars option to 0, and have GetLineTypes 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:

$('.sometextbox').focus(function() {
   if ($('.sometextbox').val().length ==0) { 
   //no text entered yet
   $('.sometextbox').val('  '); //insert 2 white spaces in order to trigger the autocomplete
}
});
笑脸一如从前 2024-11-14 03:48:16

所有答案都是正确的。我添加这个是因为 minChars 在新版本中已被弃用。
使用 minLength。

最小长度:0
您也可以考虑使用文本字段旁边的按钮,其作用类似于下拉菜单并切换所有可用选项。
快乐编码!

编辑
另外,由于您正在加载整个列表,因此您可以将延迟设置为 0,从而覆盖您可能提到的现有延迟行为。

$('input#textboxid').autocomplete('search','','delay',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.

$('input#textboxid').autocomplete('search','','delay',0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文