2012-07-16 85 views
0

我想显示一些城市的自动完成使用jquery,当任何一个选择城市,然后设置目的地ID隐藏字段。我正在使用Web服务来获取Ajax调用的数据。jQuery的自动完成标签价值undefined在asp.net

这里是我的web服务方法:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<BALDestinations> AuotExtenderDestination(string destinationname) 
    { 
     DataSet ds=objDestination.GetDestination(destinationname); 
     List<BALDestinations> result = new List<BALDestinations>(); 
     foreach (DataRow dr in ds.Tables[0].Rows) 
     { 
      BALDestinations b = new BALDestinations(); 
      b.City = dr["City"].ToString(); 
      b.DestinationId = dr["DestinationId"].ToString(); 
      result.Add(b); 
     } 
     return result; 
    } 

,这是我的jquery自动完成文本框扩展的

<script type="text/javascript"> 
    $(document).ready(function() { 
     SearchText(); 
     $('#btnSearch').click(function() { 
      alert($("#hiddenAllowSearch").val()); 
     }); 
    }); 
    function SearchText() { 
     $(".txtdest").autocomplete({ 
      // source: $local_source, 
      source: function (request, response) { 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: "WebService.asmx/AuotExtenderDestination", 
        data: "{'destinationname':'" + $('.txtdest').val() + "'}", 
        dataType: "json", 
        success: function (data) { 
         response(data.d);      
        }, 
        error: function (result) { 
         alert("Error"); 
        } 
       }); 
      }, 
      focus: function (event, ui) { 
       $(".txtdest").val(ui.item.label); 
       return false; 
      }, 
      select: function (event, ui) { 
       $(".txtdest").val(ui.item.label); 
       $("#hiddenAllowSearch").val(ui.item.value); 
       return false; 
      } 
     }); 
    } 
</script> 

未定义出现在文本框输入验证码,当我们键入任何有

+1

喜在我看来,我想你应该已经离开了代码在你的问题,你原来它有。现在看到这个问题的任何人都不会看到你最初是如何编写代码的,因此无法看出差异并从Cory的答案中学习。我盯着你的代码几分钟,认为它没有错,直到我意识到你已经更新了它。 – 2012-07-16 19:19:13

+0

@Bruen对不起,我已经回滚我的编辑,现在问题与原来的相同。我只是做了编辑,因为在cory答案后我面临一个问题。你可以检查cory和我对话的评论关于这个 – rahularyansharma 2012-07-16 19:50:24

+0

完全没有问题,这是一个很好的问题,我认为很多人,包括我自己都被这个回应对象绊倒了。 – 2012-07-16 19:54:33

回答

2

如果您从Web服务返回的类的属性不是labelvalue,那么jQuery Autoco完整的代码将尝试从不存在的属性读取值,因此会出现undefined问题。

如果你不想改变你的类属性,你可以设置自动完成来查看你的实际属性名称。而不是只打电话response(data.d),您可以将您的类属性labelvalue通过response功能发送之前手动映射:

response($.map(data.d, function (item) { 
    return { 
     label: item.City, 
     value: item.DestinationId 
    }; 
})); 
+0

只是'标签'和'value'。 AFAIK,'id'在jQueryUI自动完成中没有做任何特殊的事情。 – 2012-07-16 18:01:46

+0

@Cory先生我能够得到像城市名称标签,但警报警报($(“#hiddenAllowSearch”))。VAL()); 它仍然显示城市名称,但我希望destinationId – rahularyansharma 2012-07-16 18:04:03

+0

@Cory thanx其问题是,如果他们没有找到确切的价值部分,然后jquery使用标签作为价值,在我的情况下,问题是DestinationID vs DestinationId再次感谢它的完成 – rahularyansharma 2012-07-16 18:14:06

2

如果你想城市,州/省,国家的名字添加到标签,而不是一个城市的名字,那么你可以扩展你的代码,并添加额外的值到自定义对象:

服务:

foreach (DataRow dr in ds.Tables[0].Rows) 
{ 
    BALDestinations b = new BALDestinations(); 
    b.City = dr["City"].ToString(); 
    b.DestinationId = dr["DestinationId"].ToString(); 
    b.State = dr["State"].ToString(); 
    b.Province = dr["Province"].ToString(); 
    b.Country = dr["Country"].ToString(); 

    result.Add(b); 
} 

阿贾克斯success回调:

response($.map(data.d, function (item) { 

    // return custom object 
    var resultList = { 
     label: item.City + ", " + item.State + ", " + 
       item.Province + "' " + item.Country, 
     value: item.DestinationId, 

     // Alternatively, you can return the properties in 
     // this object and display them via _renderItem  
     state: item.State, 
     province: item.Province, 
     country: item.Country 
    }; 

    return resultList; 
})); 

要显示在自动完成列表中的状态,省,国家,覆盖_renderItem

$(".txtdest").autocomplete({ 
    source: function (request, response) { 
     // etc. 
    } 
}).data("autocomplete")._renderItem = function (ul, item) { 
    return $("<li></li>") 
    .data("item.autocomplete", item) 
    .append("<a><strong>" + item.value + "</strong><br>" + item.state + 
      " PROVINCE: " + item.province + " COUNTRY: " + item.country + "</a>") 
    .appendTo(ul); 
};