jQuery autocomplete manual input

喜你入骨 提交于 2019-12-14 03:23:14

问题


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.

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.


回答1:


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

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

});

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.

edit

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
            }

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



来源:https://stackoverflow.com/questions/8599621/jquery-autocomplete-manual-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!