jQuery UI 自动完成不显示通过 AJAX 获取的结果
我正在尝试在我的 Web 应用程序中使用 jQuery UI 自动完成功能。我设置的是一个名为 SearchPreload.aspx 的页面。此页面检查是否有一个值(术语)与另一个参数一起出现。该页面验证传入的值,然后从数据库中提取一些数据并在页面上打印出 javascript 数组(例如:["item1","item2"]
)。代码:
protected void Page_Load(object sender, EventArgs e)
{
string curVal;
string type ="";
if (Request.QueryString["term"] != null)
{
curVal = Request.QueryString["term"].ToString();
curVal = curVal.ToLower();
if (Request.QueryString["Type"] != null)
type = Request.QueryString["Type"].ToString();
SwitchType(type,curVal);
}
}
public string PreLoadStrings(List<string> PreLoadValues, string curVal)
{
StringBuilder sb = new StringBuilder();
if (PreLoadValues.Any())
{
sb.Append("[\"");
foreach (string str in PreLoadValues)
{
if (!string.IsNullOrEmpty(str))
{
if (str.ToLower().Contains(curVal))
sb.Append(str).Append("\",\"");
}
}
sb.Append("\"];");
Response.Write(sb.ToString());
return sb.ToString();
}
}
数据库部分工作正常,如果我通过浏览器导航到页面屏幕上,则会打印出正确的数据。
jQuery ui 自动完成编写如下:
$(".searchBox").autocomplete({
source: "SearchPreload.aspx?Type=rbChoice",
minLength: 1
});
现在,如果我的理解是正确的,每次我在搜索框中键入内容时,它都应该充当按键并触发我的源来限制数据,正确吗?当我通过 SearchPreload.aspx 代码后面的调试语句时,似乎该页面根本没有被命中。
如果我将自动完成函数包装在 .keypress 函数中,那么我会进入搜索预加载页面,但仍然没有得到任何结果。我只想在搜索框下显示结果,就像 jQuery 网站上的默认功能示例。我做错了什么?
I am trying to use the jQuery UI autocomplete feature in my web application. What I have set up is a page called SearchPreload.aspx. This page checks for a value (term) to come in along with another parameter. The page validates the values that are incoming, and then it pulls some data from the database and prints out a javascript array (ex: ["item1","item2"]
) on the page. Code:
protected void Page_Load(object sender, EventArgs e)
{
string curVal;
string type ="";
if (Request.QueryString["term"] != null)
{
curVal = Request.QueryString["term"].ToString();
curVal = curVal.ToLower();
if (Request.QueryString["Type"] != null)
type = Request.QueryString["Type"].ToString();
SwitchType(type,curVal);
}
}
public string PreLoadStrings(List<string> PreLoadValues, string curVal)
{
StringBuilder sb = new StringBuilder();
if (PreLoadValues.Any())
{
sb.Append("[\"");
foreach (string str in PreLoadValues)
{
if (!string.IsNullOrEmpty(str))
{
if (str.ToLower().Contains(curVal))
sb.Append(str).Append("\",\"");
}
}
sb.Append("\"];");
Response.Write(sb.ToString());
return sb.ToString();
}
}
The db part is working fine and printing out the correct data on the screen of the page if I navigate to it via browser.
The jQuery ui autocomplete is written as follows:
$(".searchBox").autocomplete({
source: "SearchPreload.aspx?Type=rbChoice",
minLength: 1
});
Now if my understanding is correct, every time I type in the search box, it should act as a keypress and fire my source to limit the data correct? When I through a debug statement in SearchPreload.aspx code behind, it appears that the page is not being hit at all.
If I wrap the autocomplete function in a .keypress function, then I get into the search preload page but still I do not get any results. I just want to show the results under the search box just like the default functionality example on the jQuery website. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果服务器返回的 JSON 无效,自动完成功能将不会显示建议。因此,复制以下 URL(或返回的 JSON 数据)并将其粘贴到 JSONLint 上。查看您的 JSON 是否有效。
PS:我没有看到您正在调用
PreLoadStrings
函数。我希望这是正常的。autocomplete will NOT display suggestions if the JSON returned by the server is invalid. So copy the following URL (or the returned JSON data) and paste it on JSONLint. See if your JSON is valid.
PS: I do not see that you're calling the
PreLoadStrings
function. I hope this is normal.有几件事需要检查。
确保页面路径正确。如果您位于 http://mysite.com/subfolder/PageWithAutoComplete.aspx,并且您的搜索预加载.aspx 页面位于另一个目录中,例如 http://mysite.com/anotherFolder/searchpreload.aspx 您用作源的 url 不正确,需要为
来源:“/anotherFolder/Searchpreload.aspx?Type=rbChoice”
您可以尝试的另一件事是使您的方法正在调用 searchpreload.aspx 页面上的页面方法。通常,在使用 javascript 时,我尝试使用页面方法来处理 ajax 请求并发回其数据。有关页面方法的更多信息,请访问:http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX。 aspx
HTH.
A couple of things to check.
Make sure that the path to the page is correct. If you are at http://mysite.com/subfolder/PageWithAutoComplete.aspx, and your searchpreload.aspx page is in another directory such as http://mysite.com/anotherFolder/searchpreload.aspx the url that you are using as the source would be incorrect, it would need to be
source: "/anotherFolder/Searchpreload.aspx?Type=rbChoice"
One other thing that you could try is to make the method that you are calling a page method on the searchpreload.aspx page. Typically when working with javascript, I try to use page methods to handle ajax reqeusts and send back it's data. More on page methods can be found here: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
HTH.