[英]autocomplete is not working help is required
I have applied this upon an input type text, but it is not working. 我已经将其应用于输入类型的文本,但是它不起作用。 I need some guidance to show me where I am going wrong.
我需要一些指导以告诉我我要去哪里。 This is javascript code and is not working:
这是javascript代码,无法正常工作:
$(document).ready(function () {
$('#reasondescriptiontxtbox').autocomplete( {
source: function (request, response) {
$.ajax( {
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/getReason",
data: "{'keywords':'" + request.term + "'}",
dataType: "json",
async: true,
success: function (data) {
response(data.d);
},
error: function (result) {
//alert("Error");
}
});
},
minLength: 2
});
});
code behind: 后面的代码:
[WebMethod]
public static IList<string> getReason(string keywords)
{
int count = 0;
IList<string> result = new List<string>();
string constr
= System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(constr);
SqlCommand cmd1 = con1.CreateCommand();
cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description '%" + keywords + "%'";
try
{
con1.Open();
SqlDataReader dr = cmd1.ExecuteReader();
while (dr.Read())
{
count++;
result.Add(dr["Code_Description"].ToString());
if (count == 100)
break;
}
con1.Close();
return result;
}
catch
{
return null;
}
}
Will I have to add some sort of jQuery file? 我需要添加某种jQuery文件吗?
Your SQL code appears to have a missing like
in where Code_Description '%" + keywords + "%'"
, could that be the cause? 您的SQL代码似乎丢失了,
like
where Code_Description '%" + keywords + "%'"
,这可能是原因吗?
You'd get no results and probably an SQL exception that is being masked by your Catch. 您将不会获得任何结果,并且可能是Catch掩盖的SQL异常。
Try changing that line to 尝试将该行更改为
cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description like '%" + keywords + "%'";
If you are using Master page then your javascript code should be like this $('#<%= textbox1.ClientID %>').autocomplete({ 如果您使用的是母版页,则您的javascript代码应类似于此$('#<%= textbox1.ClientID%>')。autocomplete({
and make sure that you have included any version of jquery.js 并确保您包含了任何版本的jquery.js
I would organize unit tests into your project so you can test getReason
in isolation, with mocked input. 我会将单元测试组织到您的项目中,以便您可以通过
getReason
输入隔离地测试getReason
。 Then you can divide and conquer to determine where the problem is. 然后,您可以分而治之,以确定问题出在哪里。 If the issue is on the client side, you'll need to attach a debugger to the browser and set a breakpoint in your handler(s) to inspect locals and flow of execution.
如果问题出在客户端,则需要将调试器连接到浏览器,并在处理程序中设置断点,以检查本地变量和执行流程。
我添加了类似关键字的关键字,这也是一个问题,其次,我将其更改为: $('#reasondescriptiontxtbox').autocomplete(
更改为: $('input[id$=reasondescriptiontxtbox]').autocomplete(
现在可以正常使用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.