Ajax autocomplete (or autosuggest) with TAB completion/autofill similar to shell command line completion?

后端 未结 3 1392
孤独总比滥情好
孤独总比滥情好 2021-02-01 10:39

I\'m implementing a AJAX autocomplete/autosuggest feature, and not only do I want to do the usual show suggestions that are similar to what the user typed, but I\'d like to let

3条回答
  •  礼貌的吻别
    2021-02-01 11:29

    If your using jQuery, a great plugin is http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/. Simply use css to hide the dropdown box, and leave the tab-auto-complete functionality on.

    I think it would be simple to make a jquery plugin for yourself...

    Along the lines of Listen for the Tab Key When the tab key is pressed, trigger tab:press on input.autotab

       $(document).keydown(function(e){ if (e.keyCode = (tab-key?)){
           $('input.autotab').trigger('tab:press');
       });      
    

    Bind input.autotab's tab:press event (in an each loop... if focus==true etc.) to either a javascript array lookup, or a xhr request, (ajax), then set that input's value as the returned data.

      $('input.autotab').bind('tab:press', function(){
          if (this.hasFocus){
             this.disabled = true;
             $.get('/autotab', { q: $(this).val() }, function(response){
                   $(this).val(response);
                   this.disabled = false;
             }, function(){ this.disabled = false; });
          }
      });
    

    In your autosuggest script, write it so once the value is matched more than once in the database (use a for loop with an index, stopping at the index element where the first element is matched), and return the value up to that point.

提交回复
热议问题