'' To activate autocomplete

I am using jQuery UI Autocomplete plug-in. Is there a way I can use the search button to activate a query instead of an autocomplete text box? My users have a really bad internet connection, and auto-completion is becoming difficult for them.

+5
source share
2 answers

Yes, it can be done . To stop the search, of course, the minimum length for the search term is increased to (arbitrary) 1000 characters. At the same time, the search was automatically programmed into . Click () an event is tied to a button - this is documented on the Events tab on this page . The minimum length is set to 0 (so the search really works) before starting the search, and after the completion of autocompletion, it returns to 1000.

HTML:

<label for="tags">Tags: </label>
<input id="tags" />
<input type="button" value="Search"/>

JavaScript:

var availableTags = [
    'ActionScript',
    'AppleScript',
    'Asp',
    'BASIC',
    'C',
    'C++',
    'Clojure',
    'COBOL',
    'ColdFusion',
    'Erlang',
    'Fortran',
    'Groovy',
    'Haskell',
    'Java',
    'JavaScript',
    'Lisp',
    'Perl',
    'PHP',
    'Python',
    'Ruby',
    'Scala',
    'Scheme'
    ];

$('#tags').autocomplete({
    source: availableTags,
    minLength: 1000,
    close: function(event, ui) {
        $('#tags').autocomplete('option', 'minLength', 1000);
    }
});

$('input[type="button"]').click(function() {
    $('#tags').autocomplete('option', 'minLength', 0);
    $('#tags').autocomplete('search', $('#tags').val());
});
+7
source

The idea is to turn off autocomplete when adding text. In focus, we turn off autocomplete and turn it on by pressing a button or pressing the enter key.

<script type="text/javascript">
$(window).load(function ()
{
    // Creates the autocomplete field
    $("#lookUpField").autocomplete(
    {
        source: ["Luis", "Erick", "Jorge", "Ana", "Anabel"],
        disabled: true
    });

    //disables the search trigger when field selected
    $("#lookUpField").focus(function ()
    {
       $("#lookUpField").autocomplete("disable");
    });


    // disables the field on keypress unless the 'enter' key
    // is pressed in which case it triggers a 'search'
    $('#lookUpField').keypress(function (e)
    {
       if (e.which == 13)
       {
          lookUpData();
       }
       else
       {
           $("#lookUpField").autocomplete("disable");
       }
   });
});

function lookUpData()
{
   $("#lookUpField").autocomplete("enable");
   $("#lookUpField").autocomplete("search");
}
</script>


<div>
    <input id="lookUpField" type="text" />
    <input type="button" value="Go" onclick="lookUpData()" />
</div>
+2

All Articles