简体   繁体   中英

Jquery:create a dictionary to autocomplete all inputs

i want to add an smart autocomplete to my project in which when ever user is typing a word in any input its autocompleted from his own dictionary.

his owner dictionary is built by saving every word he ever submit to server something like (array_values($_POST))

my current JS

$('input.complete').live('keyup.autocomplete', function(){
        var hi=$(this).val().toUpperCase();
        var was=this;
        $(this).autocomplete({
//PROBLEM Should i consider to change source from ajax/mysql to different source ?
//since there gona be too many requests ??
            source: function(request, response) {
                    $.ajax({ url: '<?=base_url()?>ajax/ac',
//PROBLEM how can i set term=word currently being edited..(start=' ',end=pointerpos)
                    data: { 'term': this.term },
                    dataType: "json",
                    type: "POST",
                    success: function(data){
                        if(data.length){
                            //response(data);
          //Commented out cause i dont wana display dropdown.. just typeahead.
                              if(data[0]['value'].substr(0,hi.length).toUpperCase()==hi){
                                $(was).val(data[0]['value']);
//currently working with single word inputs..once i get how to select only current word will edit these..
                                was.selectionStart=hi.length;
                                was.selectionEnd=data[0]['value'].length;   
                            }
                        }
                    }
                });
            },
            select: function(event, ui){},
            minLength: 2,
            delay: 500
        });

As u can see i have 2 problems

Question

  1. how can i select current word that user is typing ?
  2. is this a good approach to reach my goal, or i should consider different plugin

You are using PHP language as well. So in my opinion you can use PHP to solve your problem more easily. Let's say you have php function get_word($excerpt) in get.php file. So,

<?php
     get_word($_POST['excerpt']);
     function get_word($excerpt) {
          // Find exact word from database or array using given $excerpt
          // and return complete word.
          return $complete_word;
     }
?>

And in your jquery (assuming input field as .input),

$(document).ready(function() {
     $('.input').live('keyup',function() {
          var excerpt = $(this).val();
          $.post("get.php", {excerpt:excerpt}, function(data) {
               // do anything with data.
               $('.input').val(data);
          });
     });
})

For more precise, you can get bunch of matching words from get.php file, and display list of words to be selected.

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