从下拉列表中选择后,停止 jQuery 自动完成将输入字段聚焦

发布于 2024-09-27 09:56:20 字数 543 浏览 3 评论 0原文

我正在使用 http://bassistance.de/jquery- 的 jQuery 自动完成插件插件/jquery-plugin-autocomplete/ 当用户从下拉列表中选择结果时,输入字段将获得焦点。我怎样才能阻止插件这样做? 我使用这篇文章中的提示使其工作: jQuery 自动完成插件不聚焦下一个单击的字段 但这仅在用户使用鼠标选择结果时才有效。然后输入字段不会获得焦点。但是当用户使用 ENTER 键或 TAB 键时,输入仍然获得焦点。

任何人都知道我如何更改此设置,以便当用户从下拉列表中选择一个值时,输入字段不会获得焦点?

预先非常感谢您!

问候,金

I'm using the jQuery autocomplete plugin from http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
When a user selects a result from the dropdown, the input field gets focused. How can I stop the plugin from doing that?
I made it work using the tip from this post: jQuery autocomplete plugin not focusing the next clicked field
But this only works when the user uses the mouse to select the results. Then the input field doesn't get focussed. But when the user uses the ENTER-key or the TAB-key, the input still get's focus.

Anyone knows how I can change this so that when a user has selected a value from the dropdown, the input field ddoen't get focussed?

Thank you very much in advance!

Regards, Kim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

青朷 2024-10-04 09:56:20

从插件的 selectCurrent() 函数中这一行的外观来看:

$input.trigger("result", [selected.data, selected.value]);

...每当通过鼠标或键盘选择选项时,自动完成输入上就会触发“结果”事件。

您可以绑定到同一事件并将焦点放在表单中的下一个控件上(我认为这就是您想要的,而不是仅仅从当前控件中删除焦点?):

$("input").bind("result", function () {

   // Get all controls on the form
   var controls = $(this).closest("form").find("input, textarea, select, button");

   // Find the index of the next control
   var nextIndex = controls.index(this) + 1;

   // Check if this is already the last control, so wrap around to the first one
   if (nextIndex >= controls.length) nextIndex = 0;

   // Put focus on the "next" control
   controls.eq(nextIndex).focus();

});

By the looks of this line in the plugin's selectCurrent() function:

$input.trigger("result", [selected.data, selected.value]);

...a "result" event is triggered on the autocomplete input whenever an option is selected, by mouse or by keyboard.

You can bind to that same event and put the focus on the next control in the form (which I assume is what you want, rather than just removing focus from the current one?):

$("input").bind("result", function () {

   // Get all controls on the form
   var controls = $(this).closest("form").find("input, textarea, select, button");

   // Find the index of the next control
   var nextIndex = controls.index(this) + 1;

   // Check if this is already the last control, so wrap around to the first one
   if (nextIndex >= controls.length) nextIndex = 0;

   // Put focus on the "next" control
   controls.eq(nextIndex).focus();

});
苏佲洛 2024-10-04 09:56:20

您是否尝试过 jQuery UI 的自动完成插件?问题已经解决了一半——当您按 Tab 关闭自动完成功能时,输入字段会失去焦点。当按 Enter 时,它仍然是焦点,但这可以轻松修复:

$('#myInput').keyup(function() {
  // when Enter is pressed
  if (event.which === 13) {
    $(this).blur();
  }
});

编辑

jQuery 标准化 event 对象,因此不需要将其作为参数传递。

Have you tried jQuery UI's autocomplete plugin? Half of the problem is solved – when you press Tab to close the autocomplete, the input field loses focus. When pressing Enter, it's still focused, but this can be fixed easily:

$('#myInput').keyup(function() {
  // when Enter is pressed
  if (event.which === 13) {
    $(this).blur();
  }
});

Edit

jQuery normalizes the event object, so passing it as an argument is unneccessary.

大海や 2024-10-04 09:56:20
$( "#to" ).autocomplete({
    source: availableTags,
    select: function (event, ui) {
        $( "#to" ).val( ui.item.label );
        $('#to').blur();
        return false ;
    }
});
$( "#to" ).autocomplete({
    source: availableTags,
    select: function (event, ui) {
        $( "#to" ).val( ui.item.label );
        $('#to').blur();
        return false ;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文