我对编程颇为陌生,但大多数时候我是JavaScript/jQuery的新手。我在这里的原因是因为即使我已经通过互联网搜索自动完成搜索解决方案/我尝试应用我发现的不同版本,但我无法获得实际可行的解决方案:)如何实现Javascript自动完成功能?
这里是我的一段代码:
var mydata
$(document).ready(function()
{
ConstructSuggestionArray();
$("[id$='txtSearchProject']").keypress(function()
{
$("[id$='txtSearchProject']").autocomplete
({
source: mydata
})
})
});
function ConstructSuggestionArray()
{
$.ajax
({
url: 'ProjectManagement.aspx/ConstructSuggestionArray',
type: "POST",
data: {},
async: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response)
{
if (response.d != null)
{
mydata = jQuery.parseJSON(response.d);
return true;
}
else
{
return false;
}
}
});
}
而且,一段代码,我构建阵列:
public string ConstructSuggestionArray()
{
using (DataClassesDataContext context = new DataClassesDataContext())
{
List<Utils.ProjectsOfAUser> theProjects =
ReturnProjectsAccordingToAllocation(context);
string[] projectsNameArray = new string[theProjects.Count];
int index = 0;
foreach (Utils.ProjectsOfAUser oneProject in theProjects)
{
projectsNameArray[index] = oneProject.Name;
index++;
}
string strJSON = string.Empty;
JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();
strJSON = objJSSerializer.Serialize(projectsNameArray).ToString();
return strJSON;
}
}
}
在我的项目,并在我的ASP我也补充脚本。网页。
我非常困惑,如果你能帮我弄清楚这一点,我会非常感激。
提及:txtSearchProject - 是一个asp控件。
预先感谢您。
http://jqueryui.com/demos/autocomplete/? – 2012-08-07 12:05:25
如果你在你的jQuery中指定'dataType:'json'',你可能不需要调用'jQuery.parseJSON()',因为你的'success'回调函数应该只传递一个对象而不是一个字符串。如果响应不是有效的JSON,'success'回调将不会被执行。你能发布AJAX调用的响应吗? – 2012-08-07 12:13:27
显然没有答案。我在脚本中放了一些断点(并且我使用的是IE,因为我知道我无法在其他浏览器中进行调试),但它不会去那里。对不起,我假设你听到很多愚蠢/明显的问题,但是如果我还没有尝试做一段时间的研究工作,现在我不会发布它:) – SunnyDay 2012-08-07 12:18:21