此脚本在单个Web表单中工作。
我在单独的文件中尝试了一下。...如果我调用相同的脚本,否则不起作用。
添加了WebMethod
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#txtAutoComplete").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Haha.aspx/GetCategory",
data: "{'term':'" + $("#txtAutoComplete").val() + "','term1':'" + $("#txtAutoComplete1").val() + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("Error");
}
});
}
});
$("#txtAutoComplete1").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Haha.aspx/GetCategory1",
data: "{'term':'" + $("#txtAutoComplete").val() + "','term1':'" + $("#txtAutoComplete1").val() + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("Error");
}
});
}
});
}); </script>
[WebMethod]
public static List<string> GetCategory(string term, string term1)
{
if (string.IsNullOrEmpty(term1))
term1 = " ";
List<string> result = new List<string>();
Business.ClientBusinessLayerSQL ObjMgnr = new Business.ClientBusinessLayerSQL();
DataSet ds = new DataSet();
ds = ObjMgnr.fnLocationSearch(term, term1, "GET-SEARCH-1");
foreach (DataRow row in ds.Tables[0].Rows)
{
//result.Add(ds.Tables[0].Rows[0]["LocName"].ToString());
result.Add(row["LocName"].ToString());
}
return result;
}
[WebMethod]
public static List<string> GetCategory1(string term, string term1)
{
List<string> result = new List<string>();
Business.ClientBusinessLayerSQL ObjMgnr = new Business.ClientBusinessLayerSQL();
DataSet ds = new DataSet();
ds = ObjMgnr.fnLocationSearch(term, term1, "GET-SEARCH-2");
foreach (DataRow row in ds.Tables[0].Rows)
{
result.Add(row["LocName"].ToString());
}
return result;
}
确保您具有与放置在主页中的控件的文本框相同的ID,继承了contentplaceholder id.so此使用 ClientIDMode="Static"
。同样,当您通过ajax调用数据时,参数名称及其值应为 in quotes
,否则您应该在ajax呼叫中和webMethod should be same
中使用 JSON.stringify
和 name of parameters
。并且您的WebMethods
应为static
。
您的自动完成JS没有加载。您是直接从互联网引用的。下载文件并放置在您的项目中,然后从那里引用。