有人可以向我解释一下 JQuery 自动完成的回调函数是如何工作的吗
我正在尝试让 JQuery 自动完成功能与表单一起使用,您可以在其中搜索对象的名称,并使用所选结果完成整个对象字段。
当我只需要返回一个字符串时,自动完成功能运行良好,但现在我需要返回一个更复杂的对象。
我尝试使用 json 和由管道分隔的字符串来完成此操作。从响应返回的字符串很好,但自动完成功能未显示任何结果。
有人可以告诉我应该使用什么回调将结果拆分为字符串数组,以及如何访问和格式化这些字符串以显示它们?
另外,不同回调函数的参数是什么,例如 parse、formatItem、formatMatch、formatResult 等。我真的很困惑,因为我读过的几乎每一篇文章的做法都不同,而且没有人解释实际发生的情况。
为什么我无法在 firebug 中调试回调?代码永远不会到达我的断点
这是我的 javascript
$(document).ready(
function ()
{
$("input#Venue_Name").autocomplete
(
// data comes back in format VenueId|Name|AddressLine1|AddressLine2|City|PostCode|Country
'<%= Url.Action("FindVenuesComplex","Ajax", new {@area=""}) %>',
{
mustMatch: false,
parse: function (data)
{
return data.split('|');
},
formatItem: function (data)
{
return data[1];
},
formatMatch: function (data)
{
return data[1];
},
formatResult: function (data)
{
return data[1];
}
}
).result
(
function (event, data, formatted)
{
if (data)
{
$("input#Venue_AddressLine1").val(data[2]);
}
}
);
}
);
I'm trying to get JQuery autocomplete to work with a form whereby you search for the name of an object and the whole object fields are completed with the selected result.
I had autocomplete working well when I just needed to return a string but now I need to return a more complex object.
I've tried to do it with json, and with a string seperated by pipes. The String coming back from the response is fine but autocomplete is not displaying any results.
Can someone tell me what callback I should be using to split my results into an array of strings and how I then access and format those strings for the purpose of displaying them?
Also, what are the parameters for the different callback functions e.g. parse, formatItem, formatMatch, formatResult etc. I'm really confused because nearly every article I've read does it differently and no-one explains what is actually happening.
Why cant I debug a callback in firebug? The code never hits my breakpoints
Here's my javascript
$(document).ready(
function ()
{
$("input#Venue_Name").autocomplete
(
// data comes back in format VenueId|Name|AddressLine1|AddressLine2|City|PostCode|Country
'<%= Url.Action("FindVenuesComplex","Ajax", new {@area=""}) %>',
{
mustMatch: false,
parse: function (data)
{
return data.split('|');
},
formatItem: function (data)
{
return data[1];
},
formatMatch: function (data)
{
return data[1];
},
formatResult: function (data)
{
return data[1];
}
}
).result
(
function (event, data, formatted)
{
if (data)
{
$("input#Venue_AddressLine1").val(data[2]);
}
}
);
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚发生了什么事,我想我会分享以防其他人遇到同样的问题。
我使用的是 JQuery Autocomplete 插件,该插件现已替换为 Jquery.UI.AutoComplete,它现在是核心 JQuery.UI 的一部分,并且预期的参数有所不同。
我现在使用的是较新的版本。
http://theycallmemrjames.blogspot.com/2010/03 /jquery-autocomplete-with-aspnet-mvc.html 对于解释如何正确使用 JQuery.UI 版本非常有帮助
I figured out what was going on and I thought I'd share in case anyone else had the same problem.
I was using the JQuery Autocomplete plugin which has now been replaced with Jquery.UI.AutoComplete which is now part of the core JQuery.UI and the params expected are different.
I'm now using the newer version.
http://theycallmemrjames.blogspot.com/2010/03/jquery-autocomplete-with-aspnet-mvc.html was very helpful in explaining how to use the JQuery.UI version properly