【问题标题】:JQuery : How to call autocomplete function on focus?JQuery:如何在焦点上调用自动完成功能?
【发布时间】:2017-10-16 11:07:57
【问题描述】:

我有一个自动完成功能,它工作得很好,但现在我要做的是调用自动完成功能 onfocus,我试过但没有工作。

下面是我的代码:

function myAutoComplete() {

        var label = "No Results";
        var error="Error";

        $("#myForm\\:autocomplete").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "#{request.context}/rest/myFunctionPath",
                    dataType: "json",
                    type:'POST',
                    data: {
                        de : request.de,
                        id : id
                    },
                    success: function(data) {
                        if(data.length==0)
                            response([label]);
                        else
                            response(data);
                    },
                    error: function( jxhr, textStatus, errorThrown){
                        var result = [error];
                        response(result);
                    }
                });
            },
            select : function(event, ui) 
            {
                if (ui.item.label == label || ui.item.label == error) {
                    event.preventDefault();
                    return false;
                } else {

                }
            },
        }).focus(function(){            
            $(this).data("autocomplete").search($(this).val());
        });
    };

如何在文本框的焦点上调用上述自动完成功能?

当我点击文本框时出现错误

TypeError: $(...).data(...) is undefined

请指导

【问题讨论】:

  • 使用onfocus=myAutoComplete() 进行通话。请确保首先包含jquery.js

标签: javascript java jquery jquery-ui autocomplete


【解决方案1】:

尝试在焦点函数中调用 myAutoComplete()

使用这个

$( "#target" ).focus(function() {
  myAutoComplete();
});

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 2015-06-20
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多