2014-06-06 107 views
0

我寫下面的代碼。簡單地說,我想從userfunctions.php中獲取ajax的源數組。它可以工作,但幾毫秒後,選擇div就會消失。當我不使用$('#autocomplete-ajax')。autocomplete({..成功:它甚至不能工作..它看到數組爲空。我能做些什麼來解決這個問題?jquery自動完成功能無法正常工作

我使用:https://github.com/devbridge/jQuery-Autocomplete

$('#autocomplete-ajax').keyup(function(){ 
    var ara = $(this).val(); 
    var ara= ara.replace("@", ""); 
    var data = "action=get_auto_users&text="+ara; 
    var arr = new Array(); 
    $.ajax({ url: '/inc/userfunctions.php', 
     data: data, 
     type: 'post', 
     success: function(output) { 
      obj = JSON.parse(output); 
      arr = new Array(); 
      $.each(obj, function(key, value) { 
       arr.push(value[2]) 
      }); 

      $('#autocomplete-ajax').autocomplete({ 
       lookup: arr, 
       lookupFilter: function(suggestion, originalQuery, queryLowerCase) { 
        var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi'); 
        return re.test(suggestion.value); 
       }, 
       onHint: function (hint) { 
        $('#autocomplete-ajax-x').val(hint); 
       } 
      }); 

     } 
    }); 

}); 

回答

0

改變它像下面和callauto功能的底部將重點工作

$('#autocomplete-ajax').keyup(function(){ 
    var ara = $(this).val(); 
    var ara= ara.replace("@", ""); 
    var data = "action=get_auto_users&text="+ara; 
    $.ajax({ url: '/inc/userfunctions.php', 
     data: data, 
     type: 'post', 
     success: function(output) { 
      obj = JSON.parse(output); 
      arr = new Array(); 
      $.each(obj, function(key, value){ 
       arr.push(value[2]); 
      }); 
      callauto(arr); 

     } 
    }); 
}); 

function callauto(arr){ 
    $('#autocomplete-ajax').autocomplete({ 
     lookup: arr, 
     lookupFilter: function(suggestion, originalQuery, queryLowerCase) { 
      var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi'); 
      return re.test(suggestion.value); 
     }, 
     onHint: function (hint) { 
      $('#autocomplete-ajax-x').val(hint); 
     } 
    }); 
    $('#autocomplete-ajax').focus(); 
} 
相關問題