简体   繁体   中英

JQuery : How to call autocomplete function on focus?

I have an autocomplete function that works really fine but now what i am tryin to do is call the autocomplete function onfocus which i tried but is not working.

Below is my code:

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());
        });
    };

How can i call the above autocomplete function on focus of textbox?.

When i click on textbox i get error

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

Please guide

try to call myAutoComplete() inside the focus function

or

use this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM