【发布时间】:2014-11-17 10:37:20
【问题描述】:
##关于自动完成文本框##
在下面的代码中,我希望自动完成文本框首先从数据库中搜索,如果从数据库中找不到数据,则从 asp.net 中的 google api 搜索,但在这种情况下,从数据库和 google api 搜索数据。
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
$('#Address').autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Root.aspx/GetAutoCompleteData",
data: "{'Address':'" + document.getElementById('Address').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
if (response(data.d) == null) {
var svalue = /** @type {HTMLInputElement} */(document.getElementById('Address'));
var svalueBox = new google.maps.places.SearchBox(/** @type {HTMLInputElement} */(svalue));
}
},
error: function (result) {
alert("Error");
}
});
}
});
});
<input id="Address" type="text" style="width: 305px" placeholder="From..."/>
[WebMethod]
public static List<string> GetAutoCompleteData(string Address)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT Address from Locations where Address LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", Address);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["Address"].ToString());
}
return result;
}
}
}
【问题讨论】:
-
这里没有明确的问题。有错误吗?是 JS 还是 ASP 的问题?请澄清一下,我会帮你的。
-
不清楚的问题Try here 可能对你有用。
-
当我想在文本框中输入地名时,他建议我先从数据库中输入地名,如果数据库中不存在有关地点,则建议我从谷歌中输入地点。
-
当表中没有任何记录时,我想从谷歌搜索。这是我的要求。
标签: javascript asp.net ajax jquery-ui