自动完成不激活

这一切看起来都不错,我提供了3个部分服务工作正常,如果查询。

///  /// Summary description for AutoCompleteService ///  [WebService(Namespace = "http://schemas")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class AutoCompleteService : BaseWebService { ///  /// Gets or sets the account finder service. ///  public ICheckoutService CheckOutService { get; set; } ///  /// Finds all addresses matching either the postcode or suburb given in the prefix text. ///  /// The prefix text. /// The count. /// Aray of address details [WebMethod] public string[] FindAddresses(string prefixText, int count) { //Check the parameters if (count == 0) count = 10; var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText); return suburbs.Take(count).ToArray(); } } 

其次是javascript

  $(function () { $("#postcode").autocomplete({ source: "AutoCompleteService.asmx/FindAddresses", minLength: 1, select: function (event, ui) { $(this).val(ui.item.value); } }); });  

其次是文本框

   

响应

  ABBARIVER,WA,6280ABBEY,WA,6280ACTONPARK,WA,6280string>ADAMSVALE,WA,6375 

好吧我做了以下改动而不是所有的指示我正确的方向,并为我提供了一个起点,也为此给了我错误信息,这有助于修改如下。

  [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string[] FindAddresses(string prefixText) { //Check the parameters var suburbs = CheckOutService.FindMatchingForPartialAddress(prefixText); return suburbs.ToArray(); } $(document).ready(function () { $('[ID$=postcode]').live('keyup.autocomplete', function () { $(this).autocomplete({ source: function (request, response) { alert(request.term); $.ajax({ url: '', data: "{ prefixText:'" + request.term + "'}", dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success: function (data) { response($.map(data.d, function (item) { return { label: item.split('-')[0], val: item.split('-')[1] } })) }, error: function (response) { alert(response.responseText); }, failure: function (response) { alert(response.responseText); } }); }, select: function (e, i) { }, minLength: 1 }); }); }); 

尝试以下代码。 这对我有用..

  $(document).ready(function () { $('[ID$=txtLastname]').live('keyup.autocomplete', function () { $(this).autocomplete({ source: function (request, response) { $.ajax({ url: '<%=ResolveUrl("~/Resources/WebService.asmx/LastName") %>', data: "{ 'prefix': '" + request.term + "'}", dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success: function (data) { response($.map(data.d, function (item) { return { label: item.split('-')[0], val: item.split('-')[1] } })) }, error: function (response) { alert(response.responseText); }, failure: function (response) { alert(response.responseText); } }); }, select: function (e, i) { }, minLength: 1 }); }); 

Web方法是

  [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string[] LastName(string prefix) { List customers = new List(); using (SqlConnection conn = new SqlConnection()) { string connectionstring = CCMMUtility.GetCacheForWholeApplication(); conn.ConnectionString = connectionstring; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select distinct top(10) Lastname from tblusers where " + "Lastname like '%'+ @SearchText + '%' order by Lastname"; cmd.Parameters.AddWithValue("@SearchText", prefix); cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { customers.Add(string.Format("{0}", sdr["Lastname"])); } } conn.Close(); } return customers.ToArray(); } } 

如果自动完成是JQueryUI,并且您希望为自动完成提供一些自定义参数,那么您将需要提供一个设置源的函数。 看看这里这应该可以帮助您定义它。

另外正如@ Shadow-Wizzard所说,你可能想确保你拥有正确的身份证