jQuery自动完成手动输入 [英] jQuery autocomplete manual input

查看:114
本文介绍了jQuery自动完成手动输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景1:用户开始输入一些单词,自动完成引擎显示建议列表(在ajax请求之后).用户单击该列表中的某个项目.该选择事件已被触发.一切都很好.

here is the scenario 1: user starts to type some word, autocomplete engine shows the suggests list (after ajax request). User clicks on some item from that list. The select event has been fired. Everything is fine.

方案2:用户开始输入一些单词,自动完成引擎显示建议列表(在ajax请求之后).但是这次用户没有单击项目,而是转到另一个字段. select事件尚未触发.但是用户输入的值是正确的.如何手动触发选择事件? 谢谢.

Scenario 2: user starts to type some word, autocomplete engine shows the suggests list (after ajax request). But this time user does no click on item and goes to another field. The select event has not been fired. But the value user entered is correct. How can I trigger select event manually? Thank you.

推荐答案

$(document).on('blur', "#inputField", function () {

  $(this).trigger('autocompleteselect');

});

根据Karthik的建议,有一个解释:为输入框的blur事件绑定一个处理程序,以便用户离开字段时运行.然后,此函数调用触发自动完成选择"事件,该事件在理论上也应绑定到此输入框.既然nKognito说这行不通...很好,我想我需要查看更多代码才能进一步进行故障排除.

From Karthik's suggestion, an explanation: bind a handler for the input box's blur event, so that when the user leaves the field, it runs. This function then calls triggers the 'autocompleteselect' event that should (in theory) be also bound to this input box. Since nKognito says this isn't working... well, I guess I need to see some more code before I can troubleshoot further.

编辑

好的,基于您的jsfiddle,我已经尝试过了,但是未能触发autocompleteselect起作用.所以现在,我建议改为:

Okay, based on your jsfiddle, I've tried, but failed to get the trigger for autocompleteselect to work. So now, I suggest this instead:

var list = [{id:1,Name:"John"},{id:2,Name:"Johna"}];
$('#a').autocomplete({
            source: function(request, response) {
                response($.map(list, function(item) {
                            return {id : item.id, label : item.Name};
                        }));
            },
            minLength: 2,
            select: function () { selectHanlder(this) }
        }).blur(function () { selectHandler(this) });


 function selectHandler(this) {
                // do what you will
            }

这将在blur事件上简单地调用与autocompleteselect相同的处理程序.应该是一样的效果.

This will simply call the same handler as the autocompleteselect, on the blur event. Should be the same in effect.

这篇关于jQuery自动完成手动输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆