2012-10-10 31 views
0

http://jqueryui.com/autocomplete/#remote自動完成只運行一次,當頁面加載或一個信打,但只有一次

 $(document).ready(function() { 
     var Employees = function(request, response) { 
      var value1 = document.getElementById('<%= txtEmployeeID.ClientID %>').value.split(" "); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "WebService.asmx/GetEmployees", 
       data: "{'keywords':'" + value1 + "'}", 
       dataType: "json", 
       async: true, 
       success: function(data) { 
        response(data.d); 
       }, 
       error: function(result) { 
        //alert("Error"); 
       } 
      }); 
     } 
     $('#<%= txtEmployeeID.ClientID %>').autocomplete({ 
      autoFocus: true, source: Employees 
     }); 
    }); 

這是自動完成從數據庫中獲取價值的代碼。

當每個字母被輸入然後過濾值時,它會運行。

我正在尋找自動完成功能,將在啓動時加載所有員工姓名到var Employee中,然後無需重試過濾自動完成將通過自動過濾作爲功能來完成。

回答

1

Jquery自動完成函數總是從您作爲參數傳遞給它的源進行過濾。如果您將數組作爲源傳遞,它總是在該數組中進行過濾。

就你而言,源代碼是一個函數,只要你輸入,它就會獲取一個新的數組(過濾數組)。

所有您需要做的就是獲取數組並存儲在變量中,然後將該數組傳遞給自動完成插件。

你可以試試這個

$(document).ready(function() { 
    var Employees = []; 
    var value1 = document.getElementById('<%= txtEmployeeID.ClientID %>').value.split(" "); 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "WebService.asmx/GetEmployees", 
      dataType: "json", 
      async: true, 
      success: function(data) { 
       Employees = data.d; 
       $('#<%= txtEmployeeID.ClientID %>').autocomplete({ 
        autoFocus: true, source: Employees 
       }); 
      }, 
      error: function(result) { 
       //alert("Error"); 
      } 
     }); 

}); 
1

添加選項選擇

select(event, ui):function(){ 
$(this).disable(); 
} 
2

你可以獲取員工,結果存儲在數組中,並提供這個數組作爲option source