jQuery UI Autocomplete disable Select & Close events

前端 未结 5 1932
离开以前
离开以前 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条回答
  •  萌比男神i
    2020-12-06 10:46

    I went down a slightly different route for this and expanded on Andrew's fiddle

    The purpose being that I always wanted the auto-complete to show whilst a certain input had focus - allowing for multiple selections.

    $("#myInput").autocomplete({
        source: ["Test", "This", "Doesnt", "Close"],
        minLength: 0,
        select: function (event, ui) {
            // Add your own custom login to manipulate ui.item.label and add what you need the input field (and ui.item.value if required.)
            // We've customised how we want the select to "work" so prevent the default
            // of auto clearing the input.    
            event.preventDefault();
        },
        close : function(event)
        {
            // We're closing the autocomplete - check if the input still has focus...
            if ($("#myInput").is(":focus"))
            {
                // Prevent the auto complete from closing.
                event.preventDefault();
    
                // Make sure we're reshowing the autcomplete - since the input would have momentarily
                // lost focus when we selected an item.
                $("#myInput").autocomplete("search", "")
            }        
        }
    });
    
    $("#myInput").focus(function () {
        // We're not taking any filtering into account for this example.
        $(this).autocomplete("search", "")
    });
    

提交回复
热议问题