自动完成功能不会在第一个输入上触发 [英] Autocomplete not firing on first input

查看:60
本文介绍了自动完成功能不会在第一个输入上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经在Google上搜索,搜索,到处搜索,但找不到答案.我正在使用jQuery UI自动完成功能来搜索项目ID编号列表.它们的长度可以是1-99999.

so I've googled, searched here, went everywhere, and I can't find an answer. I'm using jQuery UI autocomplete to search a list of item ids numbers. They can be any length from 1 - 99999.

问题:

例如,当我键入'2'时,什么也没发生,然后我按了退格键并再次键入'2',这一次,下面以2开头的所有内容都会出现,并且该事件显示在开发人员工具的网络列表"中.此外,当我搜索1时,仅显示1,但它没有执行搜索,只是吐出了输入值.但是当我按退格键并再次键入1时,搜索将触发,并且得到的结果与2相同.通常,无论我键入什么数字,默认情况下都应该触发,不是吗?我该如何纠正这个问题,并让我的自动填充功能正常运行.

When I type '2' for example, nothing happens, then I hit backspace and type '2' again and this time, anything starting with 2 appears below, and the event shows up in the "Network List" on developer's tools. Furthermore, when I search 1, only 1 shows up, but it's not performing the search it's just spitting back the input value. but when i hit backspace and type in 1 again, the search fires and I get the same results as I did with 2. Normally no matter what number I type it should just fire by default shouldn't it? How do I correct this and get my autocomplete working properly.

代码:

    $('.item-inventory').on('keyup', '#item_num', function(){
        //var search = $(this);
        var itemname;
        var itemprice;
        var itemdesc;
        $(this).autocomplete({
            delay : 100,
            source: function(request, response){
                $.ajax({
                    url: '../assets/server/orders/item_search.php',
                    dataType: 'JSON',
                    data: {search: request.term},
                    success: function (data){
                        var keys = data.search;
                    //  $('#item-name').val(data.search[0].item);
                    //  itemPrice = data.search[0].price;
                    //  itemDesc = data.search[0].desc;
                        response($.map( keys, function(i){
                            return{
                                label : i.id
                            };

                        }));

                    }
                });
            },
            minLength : 1
        });
        //$('#item-search-results').removeClass('hidden');
        $(this).on('autocompleteselect', function(event, ui){
            $('#item-search-results').removeClass('hidden');
        });
    });

推荐答案

基本上,您是在第一次按键后连接自动完成功能(对于第一次按键来说为时已晚).您还需要在每次按键时重新连接它.

Basically you are connecting the autocomplete after the first keypress (which is too late for the first keypress). You are also reconnecting it on every keypress.

您需要在启动时将自动完成功能连接到任何相关元素.

You need to connect the autocomplete once at startup to any relevant element.

例如将此放置在密钥处理程序之外:

e.g. place this outside of the key handler:

    $('.item-inventory #item_num').autocomplete({
        delay : 100,
        source: function(request, response){
            $.ajax({
                url: '../assets/server/orders/item_search.php',
                dataType: 'JSON',
                data: {search: request.term},
                success: function (data){
                    var keys = data.search;
                //  $('#item-name').val(data.search[0].item);
                //  itemPrice = data.search[0].price;
                //  itemDesc = data.search[0].desc;
                    response($.map( keys, function(i){
                        return{
                            label : i.id
                        };

                    }));

                }
            });
        },
        minLength : 1
    });

这篇关于自动完成功能不会在第一个输入上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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