我想显示一些城市的自动完成使用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>
未定义出现在文本框输入验证码,当我们键入任何有
喜在我看来,我想你应该已经离开了代码在你的问题,你原来它有。现在看到这个问题的任何人都不会看到你最初是如何编写代码的,因此无法看出差异并从Cory的答案中学习。我盯着你的代码几分钟,认为它没有错,直到我意识到你已经更新了它。 – 2012-07-16 19:19:13
@Bruen对不起,我已经回滚我的编辑,现在问题与原来的相同。我只是做了编辑,因为在cory答案后我面临一个问题。你可以检查cory和我对话的评论关于这个 – rahularyansharma 2012-07-16 19:50:24
完全没有问题,这是一个很好的问题,我认为很多人,包括我自己都被这个回应对象绊倒了。 – 2012-07-16 19:54:33