2015-06-24 52 views
0

自動完成功能不適用於鍵盤按鍵,但它適用於mouse.i,不能通過箭頭鍵選擇項目並輸入按鍵。 這是我的代碼。請給我建議任何想法。以及如何更改我在輸入字段中選擇的項目背景顏色?鍵盤不支持Jquery自動完成功能

var triggered = false; 
    var trigger = "@"; 
    jQuery(".inputbox").autocomplete({ 
     source: function(request, response) {     
        var term = request.term; 
        jQuery.ajax({ 
         url: "/home/friends?q="+term, 
         dataType: "json", 
         success: function(data) { 
          response($.map(data, function(item) { 
           // alert(item.value); 
           return { 
            label: item.name, 
            value: item.name       } 
          })); 
         } 
        }); 

       }, 
     search: function() { 
      if (!triggered) { 
       return false; 
      } 
     }, 
     select: function(event, ui) { 
      var text = this.value; 
      var pos = text.lastIndexOf(trigger); 

      this.value = text.substring(0, pos + trigger.length) + 
       ui.item.value; 

      triggered = false; 

      return false; 
     }, 
     focus: function() { return false; }, 
     delay: 0, 
     minLength: 0, 
     allowNewTags: false 
    }).bind("keyup", function() { 
     var text = this.value; 
     var len = text.length; 
     var last; 
     var query; 
     var index; 

     if (triggered) { 
      index = text.lastIndexOf(trigger); 
      query = text.substring(index + trigger.length); 
      $(this).autocomplete("search", query); 
     } 
     else if (len >= trigger.length) { 
      last = text.substring(len - trigger.length); 
      triggered = (last === trigger); 
     } 
    }); 
+0

使用typehead js,提供inbuit機制 –

回答