jQuery自动完成额外参数不起作用

时间:2011-12-05 10:07:00

标签: jquery vb.net autocomplete

您好我想在VB.Net中使用AutoComplete我很难找到合适的文档和示例。请让我知道我的代码有什么问题????提前致谢。

(基于stateid的自动完成位置)

的Javascript

 $(function() {
        $("#Location").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "webservice/AutoComplete.asmx/GetLocations",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    data: {                           
                        sid: $("#State").val(),
                        term: request.term
                    },
                    success: function(data) {
                        response($.map(data, function(item) {
                            return {
                                label: item.value,
                                value: item.value
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function(event, ui) {
                $('#Location').val(ui.item.label);
                $('#LocationID').val(ui.item.value);
            },
            open: function() {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function() {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });
    });

Web服务(.asmx)

 Imports System.Web.Services
 Imports System.Web.Services.Protocols
 Imports System.ComponentModel
 Imports System.Web.Script.Services
 Imports System.Web.Configuration
 Imports System.Data.SqlClient
 Imports System.Web.Script.Serialization
 ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 <System.Web.Script.Services.ScriptService()> _
 <System.Web.Services.WebService(Namespace:="http://xyz/webservice/")> _
 <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <ToolboxItem(False)> _
 Public Class AutoComplete
     Inherits System.Web.Services.WebService
     Dim _objJavaScriptSerializer As JavaScriptSerializer
     Public Class Location
         Public value As String
         Public text As String
         Public location As String
         Public state As String
         Public country As String
         Public postcode As String
    End Class

   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
   <WebMethod()> _
   Public Function GetLocations(ByVal term As String, ByVal sid As String) As String
       Dim _varConnectionString = WebConfigurationManager.ConnectionStrings(WebConfigurationManager.AppSettings("CommonDBConnectionStringValue").ToString()).ToString()
      Dim _objSqlConnection As New SqlConnection(_varConnectionString)
      Dim _objSqlCommand As New SqlCommand("locations", _objSqlConnection)
      _objSqlCommand.Parameters.AddWithValue("@StateID", "3")
      _objSqlCommand.Parameters.AddWithValue("@SearchTerm", term)
      _objSqlCommand.CommandType = Data.CommandType.StoredProcedure
      _objSqlConnection.Open()
      Dim _objSqlDataReader As SqlDataReader = _objSqlCommand.ExecuteReader
      Dim outputArray As New ArrayList
      While _objSqlDataReader.Read
          Dim _objLocations As New Location()
          _objLocations.value = _objSqlDataReader("LOCATIONID").ToString()
          _objLocations.text = _objSqlDataReader("LOCATION").ToString() & " " & _objSqlDataReader("STATECODE").ToString() & " " & _objSqlDataReader("POSTCODE").ToString() & " " & _objSqlDataReader("COUNTRYCODE").ToString()
         _objLocations.location = _objSqlDataReader("LOCATION").ToString()
         _objLocations.state = _objSqlDataReader("STATECODE").ToString()
         _objLocations.country = _objSqlDataReader("COUNTRYCODE").ToString()
         _objLocations.postcode = _objSqlDataReader("POSTCODE").ToString()
         outputArray.Add(_objLocations)
      End While
      _objSqlConnection.Close()
      _objJavaScriptSerializer = New JavaScriptSerializer()
      Return _objJavaScriptSerializer.Serialize(outputArray)
    End Function
 End Class

WebService返回数据

 <?xml version="1.0" encoding="utf-8" ?> 
   <string xmlns="http://xyz/webservice/">
        [{"value":"44","text":"Brisbane QLD 4000 AUS","location":"Brisbane","state":"QLD","country":"AUS","postcode":"4000"}]
   </string> 

请帮我解释这段代码中的错误:((

1 个答案:

答案 0 :(得分:0)

调用autocomplete()的select选项是在返回的JSON中查找标签属性

$('#Location').val(ui.item.label);

但它不存在。我会尝试将text(或任何你想要显示的)属性重命名为label