C# JQuery自动完成功能停止工作
我使用以下代码来实现自动完成功能。之前代码运行良好,但当我将其复制到另一个aspx页面时,代码停止工作 我已经在同一个网站上浏览了很多以前被问到的问题,但是我什么也没发现。这对我帮助不大,但不能解决我的问题C# JQuery自动完成功能停止工作,c#,jquery,asp.net,C#,Jquery,Asp.net,我使用以下代码来实现自动完成功能。之前代码运行良好,但当我将其复制到另一个aspx页面时,代码停止工作 我已经在同一个网站上浏览了很多以前被问到的问题,但是我什么也没发现。这对我帮助不大,但不能解决我的问题 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"rel="stylesheet" type="text/css"/> <script type
<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">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "PGForBoys.aspx/GetAutoCompleteData",
data: "{'location':'" + document.getElementById('ContentPlaceHolder1_txtSearch').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
$(文档).ready(函数(){
SearchText();
});
函数SearchText(){
$(“.autosuggest”).autocomplete({
来源:功能(请求、响应){
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“PGForBoys.aspx/GetAutoCompleteData”,
数据:“{'location':'”+document.getElementById('contentPlaceholder 1_txtSearch')。value+“}”,
数据类型:“json”,
成功:功能(数据){
答复(数据d);
},
错误:函数(结果){
警报(“错误”);
}
});
}
});
}
功能代码为:
[WebMethod]
public static List<string> GetAutoCompleteData(string location)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\RoomRentDatabase.mdf;Integrated Security
=True;User Instance=True"))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT Location from Details where Location LIKE '%'+@location+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@location", location);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["Location"].ToString());
}
return result;
}
}
}
[WebMethod]
公共静态列表GetAutoCompleteData(字符串位置)
{
列表结果=新列表();
使用(SqlConnection con=newsqlconnection(@“数据源=。\SQLEXPRESS;AttachDbFilename=| DataDirectory | \RoomRentDatabase.mdf;集成安全性
=True;用户实例=True”))
{
使用(SqlCommand cmd=new SqlCommand(“从详细信息中选择不同的位置,如“%”++@Location++“%”,con))
{
con.Open();
cmd.Parameters.AddWithValue(“@location”,location);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
添加(dr[“Location”].ToString());
}
返回结果;
}
}
}
我自己也犯了这个愚蠢的错误。事实上,我没有更改页面的url,因此它没有读取我的函数,因此没有显示结果
我必须更改url:“PGForBoys.aspx/GetAutoCompleteData”
我正在使用代码的每个页面。是否有错误?从服务器返回什么?data.d
中的d
是否包含这样构建的对象,{label:some text,value:some value}
?没有错误,代码在单个aspx页面上工作正常,但当我将其复制到同一网站的另一页面时,它停止工作。有没有像JQuery代码这样的东西在一个站点中只能使用一次?我已经得到了我的解决方案,感谢您的关注。请将您的解决方案作为答案发布,这样您就可以潜在地帮助其他有同样问题的人。