jquery自动完成功能无法正常工作

时间:2014-06-06 14:11:03

标签: javascript php jquery ajax autocomplete

我写了下面的代码。简单地说我想从userfunctions.php获取带有ajax的源数组..它可以工作但是在几毫秒后选择div消失了。此外,当我不使用$('#autocomplete-ajax')。autocomplete({..成功:它甚至不起作用..它看到数组为空。我该怎么做才能解决这个问题?

我正在使用:https://github.com/devbridge/jQuery-Autocomplete

$('#autocomplete-ajax').keyup(function(){
    var ara = $(this).val();
    var ara= ara.replace("@", "");
    var data = "action=get_auto_users&text="+ara;
    var arr = new Array();
    $.ajax({ url: '/inc/userfunctions.php',
        data: data,
        type: 'post',
        success: function(output) {
            obj = JSON.parse(output);
            arr = new Array();
            $.each(obj, function(key, value) {
                arr.push(value[2])
            });

            $('#autocomplete-ajax').autocomplete({
                lookup: arr,
                lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
                    var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
                    return re.test(suggestion.value);
                },
                onHint: function (hint) {
                    $('#autocomplete-ajax-x').val(hint);
                }
            });

        }
    });

});

1 个答案:

答案 0 :(得分:0)

如下所示更改它并将焦点添加到callauto功能的底部就可以了!

$('#autocomplete-ajax').keyup(function(){
    var ara = $(this).val();
    var ara= ara.replace("@", "");
    var data = "action=get_auto_users&text="+ara;
    $.ajax({ url: '/inc/userfunctions.php',
        data: data,
        type: 'post',
        success: function(output) {
            obj = JSON.parse(output);
            arr = new Array();
            $.each(obj, function(key, value){
                arr.push(value[2]);
            });
            callauto(arr);

        }
    });
});

function callauto(arr){
    $('#autocomplete-ajax').autocomplete({
        lookup: arr,
        lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
            var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
            return re.test(suggestion.value);
        },
        onHint: function (hint) {
            $('#autocomplete-ajax-x').val(hint);
        }
    });
    $('#autocomplete-ajax').focus();
}