jQuery UI Autocomplete disable Select & Close events

前端 未结 5 1957
离开以前
离开以前 2020-12-06 10:33

I\'m using jQuery UI\'s Autocomplete slightly differently than it was probably created to do.

Basically I want to keep all the same functionality, the only differenc

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 10:46

    Taking inspiration from Andrews solution, I found a way to keep autocomplete open on selection with less impact on core functionality:

    var selected;  //flag indicating a selection has taken place
    
    var $input = $("input").autocomplete({
        source: ['Hello', 'Goodbye', 'Foo', 'Bar'],
        select: function( event, ui ) {
            selected = true;
        }
    });
    
    //Override close method - see link below for details
    (function(){
       var originalCloseMethod = $input.data("autocomplete").close;
        $input.data("autocomplete").close = function(event) {
            if (!selected){
                //close requested by someone else, let it pass
                originalCloseMethod.apply( this, arguments );
            }
            selected = false;
        };
    })();
    

    So the idea is to neuter close method when appropriate, as indicated by selected flag. Having selected flag in global name space probably isn't the best idea, but that's for someone else to improve on :-).

    More about overriding methods

提交回复
热议问题